0

I'm trying to write a function which takes in a string such as String code = "<div> style="width: 3%" </div>"

I would like to replace the 3% to another number (can be more than 2 digits) so 400% etc.

I'm currently getting the charAt(21) but this is only the index of 3, so if I had 20% in that place then my code would not work.

Is there another way to replace the place where the % is stored. (also doing this by not knowing what number the current % is)

4
  • 1
    Pick up your phone and call regular expressions. Commented Aug 19, 2015 at 15:07
  • I'm just wondering why are you doing this in Java since manipulating DOM elements should be handled differently Commented Aug 19, 2015 at 15:08
  • @Lucas I believe it's for some sort of self-made CMS Commented Aug 19, 2015 at 15:09
  • Why do this in Java, not a good idea. Commented Aug 19, 2015 at 15:46

5 Answers 5

1

You can do this using a regular expression, for example:

String code = "<div> style=\"width: 3%\" </div>"
String replaced = code.replaceFirst("width: \\d+", "width: 400")

To extract the value in the existing string:

Pattern pattern =  Pattern.compile("width: (\\d+?)%");
Matcher matcher = pattern.matcher(code);
matcher.find()
matcher.group(1)//Gives 3
Sign up to request clarification or add additional context in comments.

5 Comments

would it be possible to get the current % in code using \d?
Have extended my answer to cover that
Thank you, your solution is exactly what I was looking for. I cannot up vote unfortunately.
Just one more thing. matcher.group(1) does return 3 for the use case, however what if I have 30% instead of 3%, it will still only return the first digit which is 3, but I would want the whole number 30.
Yes I have and I can only seem to get the first number so 3 if its 30%, or 4 if its 44%.
1

The best approach would be using HTML parser. But if your string will always be simple, you can use replaceAll which takes a regex:

String code = "<div> style=\"width: 3%\" </div>";
String res = code.replaceAll(yourRegex, replacement);

I will leave the full solution for you, but will give you few hints:

  • Regex tutorial
  • String#replaceAll
  • \d+ will match digits, so width:\s+\d+ will match "width" followed by space(s) and then digit(s)
  • Use parenthesis to group the caught result from the regex, for example width:\s+(\d+) will catch the result of the digits (group)

Comments

0

If your String it's:

String code = "<div> style="width: 3%" </div>"

You can find the position in which the % it's stored, like this:

int position = code.indexOf("%");

And suppossing that you have your number stored in a String:

String number = "2";

Note: It doesn't matter if it is 2 or 22 or 222.

You can get the total String with substring function:

String totalString = code.substring(0,position - 1) + number + code.substring(position+1, code.length());

And now you can print it normally:

System.out.println(totalString);

I expect it will works for you!

Comments

0

Use String#replaceFirst or String#replaceAll method: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

code = code.replaceFirst("width: ?(.*)%", "width: 400%");

Of course adapt the regex to your requirement

Comments

0

You can use HTML parser such as Jsoup.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.