-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path2-split.java
More file actions
32 lines (27 loc) · 824 Bytes
/
2-split.java
File metadata and controls
32 lines (27 loc) · 824 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
27
28
29
30
31
32
import java.util.Arrays;
import java.util.regex.Pattern;
/**
* Example of using regular expressions for splitting strings.
*/
final class Split {
/**
* Private constructor prevents class from being instantiated.
*/
private Split() {
}
/**
* Entry point of the program.
*
* @param args command-line arguments, not used
*/
public static void main(final String[] args) {
String input = "Hello World, here we are!";
// Using String API
String[] result1 = input.toLowerCase().split("\\W+");
System.out.println(Arrays.toString(result1));
// Using regex API
Pattern pattern = Pattern.compile("\\W+");
String[] result2 = pattern.split(input.toLowerCase());
System.out.println(Arrays.toString(result2));
}
}