As we all know, Java provides many ways to intercept strings. Let’s take a look at the roughly several types.
1. Split() + regular expression to intercept.
Pass the regular to split(). Returns a string array type. However, intercepting in this way will cause great performance losses, because analysis rules are very time-consuming.
String str = "abc,12,3yy98,0";String[] strs=str.split(",");for(int i=0,len=strs.length;i<len;i++){ System.out.println(strs[i].toString());}Running results:
abc
12
3yy98
0
2. Use the subString() method to perform string interception.
SubString provides different interception methods through different parameters
2.1 Only one parameter is passed
For example:
String sb = "bbbdsajjds";
sb.substring(2);
Intercept the string from the index number 2 until the end of the string. (Index value starts at 0);
2.2 Pass in 2 index values
String sb = "bbbdsajjds";
sb.substring(2, 4);
Starting from index number 2 to ending with index 4 (and not including index 4 intercept, that is, the actual intercepting characters 2 and 3);
The operation results are as follows:
bdsajjds
bd
3. Methods provided by StringUtils
StringUtils.substringBefore("dskeabcee", "e");
/The result is: dsk/
Here is the first "e" as the standard.
StringUtils.substringBeforeLast("dskeabcee", "e")
The result is: dskeabce
The last "e" is the basis here.
The above three methods of java string interception (recommended) are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.