0

I have a String of url which can be represented as

urlString = String1 + "/23px-" + String2. 

The amount of pixels is different every time (can be 23px, 19px and so on). The length of String1 and String2 unknown and varies. String1 can also contain two digits but never in combination with "px".

I tried to use the replace methods that all my urlStings have, let's say, 25px:

urlString.replace("\\d+px","25px")
urlString.replace("\\d{2}px","25px")

but it doesn't work. Where's the mistake?

2
  • 2
    You need replaceAll (replace doesn't take a regular expression). Commented Aug 15, 2016 at 2:45
  • Thank you so much. Sometimes the answer is so easy! Commented Aug 15, 2016 at 2:49

1 Answer 1

1

You were extremely close , replaceAll takes a regex. replace takes in a CharSequence/String. This works :

    String urlString = "String1" + "/23px-" + "String2";

    System.out.println(urlString.replaceAll("\\d+px", "25px"));
    System.out.println(urlString.replaceAll("\\d{2}px", "25px"));

replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement.

Sign up to request clarification or add additional context in comments.

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.