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
|
package com.string;
import java.util.Scanner;
/*
* Here we will learn to split the string based on whitespace.
*/
public class StringSplitWhiteSpace {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string to split.");
String strToSplit = scanner.nextLine();
/*
* Whitespace Character contains
* 1- \t
* 2- \n
* 3- \f
* 4- \r
*
* In combination whitespace can be represented by \s
*
* Note* : Here s is SMALL.
*/
System.out.println("Complete String : " + strToSplit);
System.out.println("String after split");
String strArr[] = strToSplit.split("\\s");
for (String str : strArr)
System.out.println(str);
}
}
|