Some strings are stored in some type name, often separated by commas,' or other symbols. If we delete a certain parameter, it is often not as convenient as arrays or lists. However, if we have the following method, we can do it well.
public class Test3 { /** * @param args */ public static void main(String[] args) { //The string to be cut String s = "123.jpg,113.jpg,121.jpg,122.jpg ,131.jpg"; String sub = ""; System.out.println("Before compilation: "+s); //Calling method sub = s.replaceAll( ",113.jpg|113.jpg,"," ");//.replaceAll( ",122.jpg|122.jpg,",""); System.out.println("After compilation: "+sub); }} Print result:
Before compilation: 123.jpg, 113.jpg, 121.jpg, 122.jpg, 131.jpg After compilation: 123.jpg, 121.jpg, 122.jpg, 131.jpg
Let’s review the replacementAll method description in JDK1.6:
replaceAll
public String replaceAll(String regex,
String replacement) Replaces all substrings that match the given regular expression with the given replacement.
The str.replaceAll(regex, repl) form that calls this method is exactly the same as the result produced by the following expression:
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that using backslashes (/) and dollar signs ($) in substitute strings may differ from the results obtained by treating them as literal substitute strings; see Matcher.replaceAll. If necessary, you can use Matcher.quoteReplacement(java.lang.String) to cancel the special meaning of these characters.
parameter:
regex - The regular expression used to match this string
replacement - The string used to replace each match returns:
The obtained String
Throw:
PatternSyntaxException - if the syntax of the regular expression is invalid