-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathSplitString.java
More file actions
35 lines (19 loc) · 691 Bytes
/
Copy pathSplitString.java
File metadata and controls
35 lines (19 loc) · 691 Bytes
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
// Java: Split a String at char
import java.util.Arrays;
class SplitString {
public static void main(String[] args) {
String s = "My dog ate my homework; Can I turn it in tomorrow?";
String[] ss = s.split(" ");
System.out.println(Arrays.toString(ss));
ss = s.split(";");
System.out.println(Arrays.toString(ss));
// you must escape special chars because the split parameter is a regex
// special chars include \ . + ^ $ | ? * ( ) [ {
String t = "54.25-128.17";
String[] tt = t.split("\\.");
System.out.println(Arrays.toString(tt));
// include multiple split chars inside brackets
tt = t.split("[.-]");
System.out.println(Arrays.toString(tt));
}
}