Cutting string methods:
If you want to cut the string at the specified tag divided into several segments, the method may be used:
-
public String[] split(String regex)
-
The cutter regex as markers, several string (array) returns after slicing
return value is an array -
Note:
Do not use a period as the English division mark.
Because the English period expressions have special meanings in regular.
The parameters regex is actually a regular expression.
If you use the English period, the cut mark should use "\.".
public class Demo05StringSplit {
public static void main(String[] args) {
String str1 = "AAA,BBB,CCC";
String[] array1 = str1.split(",");
System.out.println("数组的长度"+array1.length);
for(int i = 0;i<array1.length;i++) {
System.out.println(array1[i]);
}
String str2 = "DDD.EEE.FFF";
String[] array2 = str2.split("\\.");
System.out.println("数组的长度"+array2.length);
for(int i = 0;i<array2.length;i++) {
System.out.println(array2[i]);
}
}
}