简体   繁体   中英

How to get String replace-method to recognize for example that R35 is not R3

I have a method that replaces "R3" with a certain value within a string.

But when I have for example a string "R3 R35" which contains "R3" and "R35" and I don't want the "R3" part of "R35" to be replaced with said value, in effect getting "[replacing value]5".

I'd like my replace to recognize that R35 is not R3. Is there a regex or another way to achieve this?

You could do a regex replacement on \bR3\b :

String input = "R3 R35";
String output = input.replaceAll("\\bR3\\b", "ABC");
System.out.println(output);  // ABC R35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM