Home > Java > javaTutorial > Introduction to the method of deleting specified substrings in Java strings

Introduction to the method of deleting specified substrings in Java strings

高洛峰
Release: 2017-01-18 16:43:01
Original
2265 people have browsed it

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);
 }
 
}
Copy after login

Print results:

编译前:123.jpg,113.jpg,121.jpg,122.jpg,131.jpg
 
编译后:123.jpg,121.jpg,122.jpg,131.jpg
Copy after login

## 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:

Pattern.compile(regex).matcher(str).replaceAll(repl)

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

For more introduction to the method of deleting specified substrings in Java strings, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template