Home » Java » The Java string used to operate (search, intercept...

The Java string used to operate (search, interception, segmentation)

public class strings commonly used operations {
Public, static, void, main (String[], args) {
*
* find substring
* /
String str1= "dwqae12232aebdalf"";
/ / find the specified character position of the first occurrence
Int first1=str1.indexOf (97); / / parameters for the ASCII character code
/ / find the position of the first occurrence of the specified string
Int first2=str1.indexOf ("12");
/ / find the specified character position of the first occurrence, starting at index (including index)
Int first3=str1.indexOf (97, 0);
/ / find the position of the first occurrence of the specified string, starting at index (including index
Int first4=str1.indexOf ("12232", 0);
System.out.println ("first1=" +first1);
System.out.println ("first1=" +first2);
System.out.println ("first1=" +first3);
System.out.println ("first1=" +first4);
System.out.println ("-");
*
* intercept string
* /
To the end of / from the index (excluding index)
String substr1=str1.substring (5);
The specified interval (including the end / index)
String substr2=str1.substring (5, 10);
System.out.println ("substr1=" +substr1);
System.out.println ("substr2=" +substr2);
System.out.println ("-");
*
* split string
* /
A / / segmentation
String[] splitstr=str1.split ("a");
For (String res: splitstr) {
System.out.println (RES);
}
Note: if the character segmentation / regular expressions of the characters, you need to "escape"
}
}
Output result:
First1=3
First1=5
First1=3
First1=5
-
Substr1=12232aebdalf
Substr2=12232
-
Dwq
E12232
EBD
L

Latest