How to Split a String in Java with Delimiter?
Last Updated :
04 Mar, 2024
In Java, the split() method of String class is used to break a string into an array of substrings based on a specified delimiter. We can split a string by character or split the string by words. The best example of this is CSV (Comma Separated Values) files.
Approaches to Split a String with Delimiter
- Using split() method of the String class
- Using String Tokenizer class
- Using Scanner class
Program to Split a String with Delimiter in Java
Below are the implementations of the three approaches to split a string.
Approach 1: Using split() method in String class
The split() method is used to break a large string into part based on a specified delimiter.
Java
import java.io.*;
public class SplitExample
{
public static void main(String args[])
{
String str = " Hello, Good, Morning" ;
String[] str1 = str.split( "," );
for (String str2 : str1)
{
System.out.println(str2);
}
}
}
|
Output
Hello
Good
Morning
Explanation of the above Program:
- We declare a string variable str with the value ” Hello, Good, Morning”.
- Now, we split the string using the split() method with delimiter “,”.
- After this, we iterate through an array of string str2 and print each substring in a new line.
Approach 2: Using String Tokenizer class
The String Tokenizer class in Java is present in java.util package. The main purpose of String Tokenizer is to divide or break the given strings into several tokens based upon the delimiter.
Java
import java.util.StringTokenizer;
public class StringTokenizerExample
{
public static void main(String[] args)
{
StringTokenizer st = new StringTokenizer( "Java Programming" );
System.out.println( "Number of tokens are :" +st.countTokens());
while (st.hasMoreTokens())
{
System.out.println( "Token is : " +st.nextToken());
}
}
}
|
Output
Number of tokens are :2
Token is : Java
Token is : Programming
Explanation of the above Program:
- Here, we have created a new string Tokenizer object named st with input string “Java Programming”.
- Now, iterates through tokens using hasMoreToken() method to check if there are more tokens in the string.
- Now, inside the loop str.nextToken() is used to get the next token and print each token to console.
Approach 3: Using Scanner Class
The Scanner class in Java allow a user to read input from the keyboard. Scanner class is used to tokenize an input string. Java Scanner class is present in java.util package.
Java
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
String str = "Geeks For Geeks" ;
Scanner fromStr = new Scanner(str);
System.out.println( "Words in the string are: " );
while (fromStr.hasNext())
{
System.out.println(fromStr.next());
}
}
}
|
Output
Words in the string are:
Geeks
For
Geeks
Explanation of the above Program:
- We declare a string variable str with the value “Geeks For Geeks”.
- Now, we have created scanner class object named fromStr.
- Here, we print a message showing that the words in the string will be displayed.
- Then, we have used while to iterates through the tokens using the hasNext() method to check if there are more tokens.
- At last, it prints each word to console fromStr.next() is used to get the next token based on delimiter.
Share your thoughts in the comments
Please Login to comment...