replace part of a string in java -
i'm trying write function takes in string such string code = "<div> style="width: 3%" </div>"
i replace 3% number (can more 2 digits) 400% etc.
i'm getting charat(21) index of 3, if had 20% in place code not work.
is there way replace place % stored. (also doing not knowing number current % is)
you can using regular expression, example:
string code = "<div> style=\"width: 3%\" </div>" string replaced = code.replacefirst("width: \\d+", "width: 400")
to extract value in existing string:
pattern pattern = pattern.compile("width: (\\d+?)%"); matcher matcher = pattern.matcher(code); matcher.find() matcher.group(1)//gives 3
Comments
Post a Comment