当前位置:网站首页>String.split()最详细源码解读及注意事项
String.split()最详细源码解读及注意事项
2022-07-29 12:21:00 【m0_67392273】
前言
博主针对字符串分割时出现的各种空字符串问题,进入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方法的源码解读以及所有的注意事项,纯手打,有帮助的话麻烦给个关注+点赞收藏哟
边栏推荐
- 2.1 Bubble sort (mercifully Sorting)
- 【云原生】开源数据分析 SPL 轻松应对 T+0
- 【第三次自考】——总结
- The IDEA of Database plug-in Database Navigator plug-in
- 我和 TiDB 的故事 | 缘份在,那就终是能相遇的
- How much is the test environment, starting from the actual needs
- PHP uedtior报错 errorHandler is not defined
- 最简单的共享列表服务器KissLists
- SQL clock 】 【 daily DAY 21 丨 report the state of the system date of continuous difficulty difficult 】 【
- DAY 22 丨 daily SQL clock 】 【 the average selling price of the difficulty of medium 】
猜你喜欢

我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此

Recursion - Eight Queens Problem
Based article 】 【 learn with Rust | Rust function and process control, rounding
Go简单实现协程池
![[based] GO language. Why do I have to learn Golang and introduction to the language universal](/img/ac/80ab67505f7df52d92a206bc3dd50e.png)
[based] GO language. Why do I have to learn Golang and introduction to the language universal

AI cocoa AI frontier introduction (7.29)

【微信小程序】WXSS和全局、页面配置

TiCDC同步延迟问题处理

MySql string splitting realizes the split function (field splitting, column switching, row switching)

The IDEA of Database plug-in Database Navigator plug-in
随机推荐
XSS Vulnerability Analysis
DAY 27 daily SQL clock 】 【 丨 within a specified period of time all order products [difficult simple]
Distributed Configuration Center of Infrastructure
Based article 】 【 learn with Rust | Rust function and process control, rounding
2.2 Selection sort
WordPress 常规设置
爱可可AI前沿推介(7.29)
Chapter 2 Summary
3D激光SLAM:LeGO-LOAM论文解读---硬件系统部分
asyncawait和promise的区别
Wu En 07 regularization of teacher machine learning course notes
Js array commonly used API
别再问我如何制作甘特图了!
The adb for mysql in what platform for development
MySQL database installation (detailed)
nacos集群搭建
跨域: 汇总
Mysql各个大版本之间的区别
QCon Guangzhou Station is here!Exclusive custom backpacks are waiting for you!
Network layer and transport layer restrictions