A lot of string interception is provided in Java. Here's a look at a few of them.
1.split () + regular expression to intercept.
The regex is passed into split (). Returns a string array type. However, interception in this way can have a significant performance loss because the analysis is time-consuming.
String str = "abc,11,abc123,1"; String[] strs=str.split (","); for (int i=0,len=strs.length;i<len;i++) { System.out.println (strs[i].tostring ());}
Operation Result:
abc11abc1231
2. Use the substring () method for string interception.
SUBSTRING provides different interception methods through different parameters.
2.1 Pass only one parameter
For example:
String sb = "abcdef122d"; Sb.substring (2);
Starts the string from the index number 2 and continues to the end of the string. (index value starts from 0);
2.2 Incoming 2 index values
String sb = "abcdef122d"; Sb.substring (2,4);
Starting from index number 2 to index good 4 end (and do not include index 4 interception, that is, the actual interception of 2 and 3rd characters);
The results of the operation are as follows:
Cdef122dcd
3. Methods provided through the StringUtils
Stringutils.substringbefore ("Dskeabcee", "E");
/ Result: DSK/
Here is the first "E", as the standard.
Stringutils.substringbeforelast ("Dskeabcee", "E")
The result is: DSKEABCE
This is the last "E" to prevail.
Several ways in which Java intercepts strings