当前位置:网站首页>String.split()最详细源码解读及注意事项
String.split()最详细源码解读及注意事项
2022-07-24 02:49:00 【Killing Vibe】
前言
博主针对字符串分割时出现的各种空字符串问题,进入String类的源码看了一下,现作如下解读及演示:
一、split(regex,limit)
首先是带有两个参数的split方法:
作用:
将以给定正则表达式(regex)的字符串分隔开来
- 第一个参数是传入字符类型的分隔符,如 “,” 等(可以是任何字符串)
- 第二个参数传入整型的limit,代表的是将此字符串分割成n部分(这里的n就是limit).
返回值:
此方法返回的数组包含此字符串的每个子字符串,这些子字符串以匹配到的正则表达式(就是以输入的第一个参数regex)作为结束,或由字符串的结尾作为结束。
注意事项:
- 数组中的子字符串按照它们在这个字符串中出现的顺序排列。
- 如果输入的regex不匹配字符串里面的任何字符,那么结果数组只有一个元素,即这个字符串。(就是若字符串里面没有出现输入的regex参数)
- 如果在字符串的开头有一个正数的匹配(就是字符串开头有>0个的regex分隔符),那么在结果数组的开头会包含一个空的前导子字符串.
public class test {
public static void main(String[] args) {
String str = ",,1,2,3,4"; // 注意这里字符串开头就匹配到了逗号
String[] s = str.split(",",10);// 这里先取10,后文介绍第二个参数
for (String string : s) {
System.out.println("子字符串"+string);
}
System.out.println(s.length);
}
}
运行结果:
第一个逗号前面会有出现一个空的子字符串

- limit参数控制应用模式的次数,因此会影响结果数组的长度。(这里的意思就是limit的取值控制了结果数组的长度)

对以上解读如下:
- (1) 如果limit输入的是一个正数, 那么该模式将最多应用limit - 1次(就是说只会用输入的regex去字符串里面匹配limit-1次),数组的长度将不大于limit,并且数组的最后一个条目将包含最后一个匹配的分隔符之外的所有输入(就是说他分隔的模式是从前逐个往后的).给个代码便于大家理解:
public class test {
public static void main(String[] args) {
String str = "1,2,3,4";
String[] s = str.split(",",2);//这里输入limit为2,即分成2部分
for (String string : s) {
System.out.println("子字符串"+string);
}
System.out.println(s.length);
}
}
运行结果:
字符串被分隔成2个子字符串,分隔模式是从前往后的

- (2) 如果输入的limit为零,则模式将被应用尽可能多的次数,结果数组可以有任何长度,而尾部的空字符串将被丢弃. (就是匹配字符串里面所有的regex分隔符),关于空字符串被丢弃,代码如下:
public class test {
public static void main(String[] args) {
String str = "1,2,3,4,,,";// 这里后面逗号之间的空字符串将被丢弃
String[] s = str.split(",",0);
for (String string : s) {
System.out.println("子字符串"+string);
}
System.out.println(s.length);
}
}
运行结果:
尾部的空字符串将不会出现在结果数组里

- (3) 如果输入limit的值为负数,则模式将被应用尽可能多的次数,数组可以有任何长度。(尾部的空字符串也不会被丢失噢)
public class test {
public static void main(String[] args) {
String str = ",1,2,3,4,";
String[] s = str.split(",",-1);//limit值为负数
for (String string : s) {
System.out.println("子字符串"+string);
}
System.out.println(s.length);
}
}
运行结果:
字符串的尾部空字符串不会被丢失

二、split(regex)
接下来只带有一个参数的split方法就容易了,就是默认limit的值为0.

该方法的工作原理就是用给定regex参数和一个limit参数默认为0来调用两个参数的split方法。因此,结果数组中不包含尾随的空字符串。

总结
以上就是对String类中split方法的源码解读以及所有的注意事项,纯手打,有帮助的话麻烦给个关注+点赞收藏哟
边栏推荐
- The practical influence of educational robot on students' learning effect
- Only beautiful ones can be opened
- X Actual combat - Cloud Server
- PMP preparation experience | good habits, good process, good results
- Vscade connects to the server. The password is correct, but it has been unable to connect
- I'm a novice. I heard that there is a breakeven financial product in opening an account. What is it?
- summernote富文本编辑器
- Openresty Lua resty balancer dynamic load balancing
- C language exercises
- 通用机环境下安全版单机数据库使用非root用户管理的解决方案
猜你喜欢

Unity timeline tutorial
![js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标](/img/4e/3d0c25d9579b6d5c00473048dbbd83.png)
js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标

Essential skills for programmers -- breakpoint debugging (idea version)

Live800: there is nothing trivial about customer service. Don't let service destroy the reputation of the enterprise

Share an API Gateway project based on ABP and yarp
![JS when transferring parameters, the incoming string has data; No data when number is passed in; 2[0] is right! Number type data can be subscripted](/img/4e/3d0c25d9579b6d5c00473048dbbd83.png)
JS when transferring parameters, the incoming string has data; No data when number is passed in; 2[0] is right! Number type data can be subscripted

Analyze the space practice field required by maker education activities

攻防世界WEB练习区(backup、cookie、disabled_button)

22 -- range and of binary search tree

Camper recruitment | AI youth with the world in mind, the United Nations needs your help for sustainable development!
随机推荐
SkyWalking分布式系统应用程序性能监控工具-上
"Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture
SSM家庭理财个人理财管理系统记账系统
22 -- 二叉搜索树的范围和
Why use the well architected framework?
Doodle Icons - 一组免费商用的涂鸦风格图标库,可爱轻快又独特
[management / upgrade] * 02. View the upgrade path * FortiGate firewall
Ugui source code analysis - stencilmaterial
Is Zhongcheng hospital really helping suppliers solve problems?
LeetCode-栈和队列刷题
Go IO operation - file read
软考---程序设计语言基础(上)
理解加载class到JVM的时机
go log包
Interpretation of steam education with the deepening of educational reform
Summernote rich text editor
Camper recruitment | AI youth with the world in mind, the United Nations needs your help for sustainable development!
攻防世界WEB练习区(webshell、command_execution、simple_js)
C from zero
Job hunting and recruitment system of SSM part-time job hunting