Some strings are where we store names of certain types, often separated by commas ',' or other symbols. If we delete a parameter, it is often not as convenient as an array or list. However, if we have the following method, we can also do it well.
public class Test3 { /** * @param args */ public static void main(String[] args) { //要切割的字符串 String s = "123.jpg,113.jpg,121.jpg,122.jpg,131.jpg"; String sub = ""; System.out.println("编译前:"+s); //调用方法 sub = s.replaceAll( ",113.jpg|113.jpg,","");//.replaceAll( ",122.jpg|122.jpg,",""); System.out.println("编译后:"+sub); } }
Print results:
编译前:123.jpg,113.jpg,121.jpg,122.jpg,131.jpg 编译后:123.jpg,121.jpg,122.jpg,131.jpg## Let’s review the replaceAll method description in JDK1.6:
Replaceall
Public String Replaceall (String RegX,
String Replacement) uses a given replacement to replace all the presented sub -string of the given regular expression.
The str.replaceAll(regex, repl) form of calling this method produces exactly the same result as the following expression:
Note that using backslashes (\) and dollar signs ($) in a replacement string may have different results than treating it as a literal replacement string; see Matcher.replaceAll. If necessary, you can use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters.
Parameters:
regex - the regular expression used to match this string
replacement - the string used to replace each match
Returns:
The resulting String
Throw :
PatternSyntaxException - If the syntax of the regular expression is invalid