当前位置:网站首页>String类中split()方法的使用
String类中split()方法的使用
2022-06-13 00:11:00 【番茄好困】
String类中split方法的使用
查看api会发现,split方法有方法重载
一个是split(String regex)一个是split(String regex,int limit)
先讲解split(String regex,int limit)
先看官方解释:
官方解释:
public String[] split(String regex,
int limit)将此字符串拆分为给定的regular expression的匹配。 此方法返回的数组包含此字符串的每个子字符串,该字符串由与给定表达式匹配的另一个子字符串终止,或由字符串结尾终止。
数组中的子字符串按照它们在此字符串中的顺序排列。 如果表达式与输入的任何部分不匹配,则生成的数组只有一个元素,即这个字符串。当在此字符串的开始处存在正宽度匹配时,在结果数组的开始处包含空的前导子字符串。 开始时的零宽度匹配不会产生这样的空的前导子串。
limit参数控制应用模式的次数,因此影响生成的数组的长度。 如果极限n大于0,则模式最多应用n -1次,数组的长度不大于n
,数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。 如果n是非正的,那么模式将被应用到尽可能多的次数,并且数组可以有任何长度。
如果n为0,则模式将被应用尽可能多次,数组可以有任何长度,并且尾随的空字符串将被丢弃。
理解:就是将字符串中的满足regex的部分换成空字符串并且从这里分割,并且转换成数组
String Regex是一个正则表达式,limit是切割次数
现在limit存在三种情况:1.正数2.负数3.等于0
正数:
split方法会将字符串切割 (limit-1) 次,就是说如果limit =
1,那么split数组长度为1,就是原来的字符串,只不过变成了数组。还有就是regex不满足字符串中任何字符,也会把原来的字符串原封不动的转为数组,如果是2的话,那么会切割1次,如果是3的话,切割2次




负数:
split会无限次数切割,就是把字符串中所有满足regex的都替换成空字符串,转为数组
无论是-1还是-10,结果都是一样


0:
split也会无限次数切割,也把字符串中所有满足regex的都替换成空字符串,但是在字符串末尾的空字符串会被清除再转换成数组


再来看看split(String regex),就是一个默认limit是0的split(String regex,int limit)
对split()方法的理解有深了一步,nice
边栏推荐
- Explanation and practice of implicit transformation and implicit parameters in Scala
- C language standard IO, for example: fread(), fwrite(), fgetc(), etc. (end)
- How to quickly query the online status of mobile phones
- 哲學和文學的區別
- June 11, 2022 diary: Mr. Wang's spring, mixed in
- TypeError: wave.ensureState is not a function
- Enterprise wechat H5_ Authentication, PC website, enterprise wechat scanning code, authorized login
- [LeetCode]28. Implement strstr()
- PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
- Start blogging
猜你喜欢

The difference between caching and buffering

Start of u-boot S analysis (III)
![[matlab] matrix transformation and matrix evaluation](/img/71/b7614e2e4ea2dda0f44f0ea8bcbf45.png)
[matlab] matrix transformation and matrix evaluation
![[matlab] symbol calculation](/img/a5/7682a465ca2813a83114c091580c53.png)
[matlab] symbol calculation

PLC也能制作小遊戲----Codesys編寫猜數字小遊戲

How to make maputnik, a vector tile matching artifact, support GeoServer

leaflet中如何通过透明度控制layerGroup的显示隐藏

Learn to divide subnets in an article

Case sharing of online real queuing system reconfiguration -- practical part

【Matlab】矩阵变换与矩阵求值
随机推荐
TypeError: wave. ensureState is not a function
Start blogging
Do you have to read for PMP?
【Matlab】三维曲线与三维曲面
Machining Industry MES system Mold Industry MES system CNCl Medium Industry MES System MES code scanning and reporting MES data collection
leaflet中如何优雅的解决百度、高德地图的偏移问题
How to visit a website
Day 3 of jsbom and DOM learning
H5時代leaflet中還在用DivIcon?
La différence entre philosophie et littérature
[LeetCode]28. Implement strstr()
Talent Weekly - 5
新增博客地址
MySQL index
On the parameters of main function in C language
Explanation and practice of implicit transformation and implicit parameters in Scala
Huawei cloud elastic ECS use [Huawei cloud to jianzhiyuan]
【Matlab】矩阵变换与矩阵求值
How to pass the PMP review?
VHDL programming experiment exercises collection