-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitDemo.java
More file actions
54 lines (46 loc) · 1.19 KB
/
Copy pathSplitDemo.java
File metadata and controls
54 lines (46 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.string;
import org.apache.commons.lang.StringUtils;
/**
* @Author: EnjoyCoding
* @Date: 2020\3\14 0014 22:51
* @Description:
*/
public class SplitDemo {
/**
* 使用索引访问用 String 的 split 方法得到的数组时,需做最后一个分隔符后有无
* 内容的检查,否则会有抛 IndexOutOfBoundsException 的风险。
*/
public void splitStr() {
String str = "a,b,c,,";
String[] ary = str.split(",");
// 预期大于 3,结果是 3
int length = ary.length;
System.out.println(ary[length - 1]);
}
public void splitRegex() {
String str = "172.168.1.1..";
String[] part=str.split("\\.");
System.out.println(part[0]);
System.out.println(part[1]);
}
public void splitSpace() {
String str = "a b c d ";
String[] part=str.split("\\s+");
System.out.println(part[0]);
System.out.println(part[1]);
}
public void splitError(String str) {
if (StringUtils.isEmpty(str)) {
return;
}
String[] strArray = str.split(",");
// String s0 = strArray[0];
// String s1 = strArray[1];
if (strArray.length == 1) {
String s0 = strArray[0];
} else if (strArray.length > 1) {
String s0 = strArray[0];
String s1 = strArray[1];
}
}
}