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

技术
©2019-2020 Toolsou All rights reserved,
java实现抢红包功能2021年2月程序员工资统计,平均15144元可怕的不是堕落,而是清楚自己在堕落java简单的抽奖算法,抽奖DemoPYTHON入门期末复习汇总惹什么猫都别惹熊猫!「功夫熊猫」20年对人类拿下4血软件测试之BUG描述JS 的骚操作程序员因拒绝带电脑回家被开除,获赔 19.4 万元vue组件页面高度根据屏幕大小自适应