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

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -