How do you write a Java string replacement expression?
In Java, you can use the replaceAll() method to replace expressions in a string. The replaceAll() method takes two parameters, the first parameter is the expression to be replaced, which can be represented using regular expressions, and the second parameter is the string to replace it with. Here is an example:
String str = "Hello 123 World!";
String replacedStr = str.replaceAll("\\d+", "###");
System.out.println(replacedStr);
The output result is:
Hello ### World!
In the above example, the regular expression \\d+ matches one or more digits. Replace the matched numbers with “###” to get the final replaced string.
It is important to note that in Java, the replaceAll() method for strings uses regular expressions for matching and replacing, so it is necessary to write the correct regular expression according to the actual requirements. If regular expressions are not needed, consider using the replace() method for simple string replacement.