user7617828
user7617828

Reputation: 31

Replace digits in String url

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?

Upvotes: 0

Views: 145

Answers (1)

Ramachandran.A.G
Ramachandran.A.G

Reputation: 4948

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.

Upvotes: 1

Related Questions