split()方法:指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回;
给出几种代码及结果:
1、结果为0
public class TestSpit {
public static void main(String[] args) {
String str = "@";
System.out.println(str.split("@"
).length);
}
}
"E:\JAVA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\JAVA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=50176:E:\JAVA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\project\day06-code\out\production\day06-code com.lg.demo11.TestSpit
0
Process finished with exit code 0
2、结果为1
public class TestSpit {
public static void main(String[] args) {
String str = "";
System.out.println(str.split("@"
).length);
}
}
原字符串中不包含@
"E:\JAVA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\JAVA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=50461:E:\JAVA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\project\day06-code\out\production\day06-code com.lg.demo11.TestSpit
1
Process finished with exit code 0
3、结果为2
public class TestSpit {
public static void main(String[] args) {
String str = "123@123";
System.out.println(str.split("@"
).length);
}
}
"E:\JAVA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\JAVA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=50507:E:\JAVA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\project\day06-code\out\production\day06-code com.lg.demo11.TestSpit
2
Process finished with exit code 0
4、结果为3
public class TestSpit {
public static void main(String[] args) {
String str = "123@123@123";
System.out.println(str.split("@"
).length);
}
}
"E:\JAVA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\JAVA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=50544:E:\JAVA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\project\day06-code\out\production\day06-code com.lg.demo11.TestSpit
3
Process finished with exit code 0