split() method : Specify character ( strand ) Or regular to split a string , The result is returned as an array of strings ;
Several codes and results are given :
1, The result is 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, The result is 1
public class TestSpit { public static void main(String[] args) { String str =
""; System.out.println(str.split("@" ).length); } }
The original string does not contain @
"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, The result is 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, The result is 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
Technology
Daily Recommendation