Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8d3133

Browse filesBrowse files
committed
[U] 修改String源码分析中,split()方法中传入limit > 0时的错误逻辑
1 parent 1b299c6 commit a8d3133
Copy full SHA for a8d3133

File tree

Expand file treeCollapse file tree

2 files changed

+61
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+61
-8
lines changed
Open diff view settings
Collapse file
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.learnjava.string;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author LuoHaiYang
7+
*/
8+
public class StringTest {
9+
10+
@Test
11+
public void testSplitWithSplit() {
12+
String splitStr1 = "what,is,,,,split";
13+
String[] strs1 = splitStr1.split(",");
14+
for (String s : strs1) {
15+
System.out.println(s);
16+
}
17+
System.out.println(strs1.length);
18+
}
19+
20+
@Test
21+
public void testSplitWithOutSplit() {
22+
String splitStr1 = "what,is,,,,";
23+
String[] strs1 = splitStr1.split(",");
24+
for (String s : strs1) {
25+
System.out.println(s);
26+
}
27+
System.out.println(strs1.length);
28+
}
29+
30+
@Test
31+
public void testSplitLimit() {
32+
String splitStr1 = "what,is,,,,";
33+
String[] strs1 = splitStr1.split(",", -1);
34+
for (String s : strs1) {
35+
System.out.println(s);
36+
}
37+
System.out.println(strs1.length);
38+
}
39+
40+
@Test
41+
public void testSplitLimitGreaterThanZero() {
42+
String splitStr1 = "what,is,,,,";
43+
String[] strs1 = splitStr1.split(",", 3);
44+
for (String s : strs1) {
45+
System.out.println(s);
46+
}
47+
System.out.println(strs1.length);
48+
}
49+
}
Collapse file

‎note/JDK/深入学习String源码与底层(二).md‎

Copy file name to clipboardExpand all lines: note/JDK/深入学习String源码与底层(二).md
+12-8Lines changed: 12 additions & 8 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ if判断中**第一个括号**先判断一个字符的情况,并且这个字
335335
break;
336336
}
337337
}
338-
// If no match was found, return this
338+
// 如果没有匹配到的内容则直接返回
339339
if (off == 0)
340340
return new String[]{this};
341341
342-
// Add remaining segment
342+
// 添加剩余的segment到list集合中
343343
if (!limited || list.size() < limit)
344344
list.add(substring(off, value.length));
345345
@@ -441,7 +441,7 @@ is
441441
n
442442
o
443443
```
444-
list集合里就会添加进字符串is,
444+
list集合里就会添加进字符串is
445445

446446
第三次进入while循环时,此时的字符数组以及索引关系如下:
447447
```
@@ -485,7 +485,7 @@ list集合里就会添加进空字符串""
485485
[what,is, , , ,split]
486486
```
487487

488-
这里相信小伙伴们都知道示例1和示例2的去别在那里了,是因为示例2最后索引位置的list为空字符串,所以会调用下面的代码逻辑
488+
这里相信小伙伴们都知道示例1和示例2的区别在那里了,是因为示例2最后索引位置的list为空字符串,所以list.get(resultSize-1).length()为0,则会调用下面的代码逻辑
489489
```
490490
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
491491
resultSize--;
@@ -497,7 +497,7 @@ list集合里就会添加进空字符串""
497497
```
498498

499499
对于入参limit,可以总结一下为:
500-
1. limit > 0,split()方法不进行拆分,返回原字符串
500+
1. limit > 0,split()方法不进行拆分,最多把字符串拆分成limit个部分
501501
2. limit = 0,split()方法会拆分匹配到的最后一位regex。
502502
3. limit < 0,split()方法会根据regex匹配到的最后一位,如果最后一位为regex,则多添加一位空字符串;如果不是则添加regex到字符串末尾的子字符串。
503503

@@ -508,9 +508,13 @@ list集合里就会添加进空字符串""
508508
boolean limited = limit > 0; // limited为true
509509
..
510510
..
511-
if(!limited || list.size() < limit - 1) { // !limited为false
512-
...
513-
} else {// 此时执行词句
511+
// !limited为false;list.size()一开始为0,则如果limit > 2,则list.size() < limit - 1为true,则进行字符串截取subString方法调用;
512+
// 所以如果limit = 1,则直接返回原字符串;如果limit > 1则将字符串拆分为limit个部分;
513+
if(!limited || list.size() < limit - 1) {
514+
// 截取off到next字符串存入list中
515+
list.add(substring(off, next));
516+
off = next + 1;
517+
} else {
514518
list.add(substring(off, value.length));
515519
off = value.length;
516520
break;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.