Split String in Java
Split String in Java
You can split a String by whitespaces or tabs in Java by
using the split() method of java.lang.String class. This method accepts a regular expression
and you can pass a regex matching with whitespace to split the String where
words are separated by spaces.
This java program has demonstrated to split a String by single
or multiple whitespaces or tabs in Java.
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StringSplitDemo {
public static void main(String[] args) {
// TODO Auto-generated
method stub
String
line;
String[]
words;
BufferedReader
reader = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("input string
words separated by space: ");
try {
line = reader.readLine();
words = line.split(" ");
System.out.println("output
string: ");
for (int i = 0; i < words.length; i++)
System.out.println(words[i]);
}
catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
System.out.println("input string
words separated by multiple space: ");
try {
line = reader.readLine();
words = line.split("\\s+");
System.out.println("output
string: ");
for (int i = 0; i < words.length; i++)
System.out.println(words[i]);
}
catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
System.out.println("input string
with leading and trailing space: ");
try {
line = reader.readLine();
words = line.trim().split("\\s+");
System.out.println("output
string: ");
for (int i = 0; i < words.length; i++)
System.out.println(words[i]);
}
catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
}
Output:
input string words separated by space:
Hi Friends, My Name is Piyush Patel
output string:
Hi
Friends,
My
Name
is
Piyush
Patel
input string words separated by multiple
space:
Hi
Friends, My name is Piyush Patel
output string:
Hi
Friends,
My
name
is
Piyush
Patel
input string with leading and trailing space:
My Name is Piyush Patel
output string:
My
Name
is
Piyush
Patel
Important
points about split() method:
- Splits
this string around matches of the given regular expression.
- Returns
the array of strings computed by splitting this string around matches of
the given regular expression
- The split() method throws PatternSyntaxException -
if the regular expression's syntax is invalid
- This
method was added to Java 1.4, so it's not available to the earlier version
but you can use it in Java 5, 6, 7 or 8 because Java is backward
compatible.
To split string using delimiter like comma, colon, space etc.:
String mystr = "Red,Green,Yellow,Pink,Blue";
String[]
splits = mystr.split(",");
System.out.println("splits.size:
" +
splits.length);
for(String x: splits)
{
System.out.println(x);
}
Output:
splits.size: 5
Red
Green
Yellow
Pink
Blue
String split example using StringTokenizer
mystr = "1:22:333:4444:55555";
StringTokenizer stringtokenizer = new StringTokenizer(mystr, ":");
while (stringtokenizer.hasMoreElements())
{
System.out.println(stringtokenizer.nextToken());
}
Output:
1
22
333
4444
55555
Comments
Post a Comment