Java string split multiple delimiters
In this post, you will learn how to split multiple delimiters using the Java programming language. During programming, you may face some situations where you need to utilise the split method in Java to split a complete string into separate words, not to part with just one character but rather with different characters.
A delimiter is a sequence of one or more characters for specifying the boundary between separate, independent regions in plain text, mathematical expressions, or other data streams.
A delimiter is an arrangement of one or more characters used to indicate the limit between separate, independent regions in plain content, mathematics, or other information streams. Some examples of delimiters are commas, braces, brackets, etc. Delimiters address one of several different means for determining limits in an information stream.
Java string.split() method
The string split() method of Java splits this string against a given regular expression and returns a char array.
Signature of split()public String split(String regex)
and,
public String split(String regex, int limit)
Here, regex is the regular expression to be applied on string, and limit is the limit for the number of strings in an array. If it is zero, it will return all the strings matching regex.
Here is the basic example of splitting a string using a delimiter in a Java program. We split the string based on the delimiter hyphen "-".
import java.util.*;
public class SplitString
{
public static void main(String[] args)
{
String str = "welcome-to-etutorialspoint";
String[] arr = str.split("-");
System.out.println(Arrays.toString(arr));
}
}
Output of the above code:
[welcome, to, etutorialspoint]
Java split string by comma
The given Java program splits the string by comma.
import java.util.*;
public class SplitString
{
public static void main(String[] args)
{
String str = "Success,is,journey";
String[] arr = str.split(",");
System.out.println(Arrays.toString(arr));
}
}
Output of the above code:
[Success, is, journey]
Java split string by whitespace
Consecutive whitespace includes spaces, newlines \n and tabs \t, and consecutive whitespace is handled together. The given Java program splits the string by whitespace.
import java.util.*;
public class SplitString
{
public static void main(String[] args)
{
String str = "Success is journey";
String[] arr = str.split("\\s");
System.out.println(Arrays.toString(arr));
}
}
Output of the above code:
[Success, is, journey]
Java split string by multiple delimiters
We can use regex OR operator '|' symbol between multiple delimiters to split a string with multiple delimiters. Here is the basic example of splitting a string with multiple delimiters.
import java.util.*;
public class StringExample
{
public static void main(String[] args)
{
String str = "welcome-to-etp. etutorials-point.";
String[] strArray = str.split("-|\\.");
System.out.println(Arrays.toString(strArray));
}
}
Output of the above code:
[welcome, to, etp, etutorials, point]
Java String split(String regex, int limit)
The given Java program demonstrates the working of string split with limit.
import java.util.*;
public class StringExample
{
public static void main(String[] args)
{
String str = "Success usually comes to those who are too busy to be looking for it.";
String[] strArray = str.split("\\s", 5);
System.out.println(Arrays.toString(strArray));
}
}
Output of the above code:
[Success, usually, comes, to, those who are too busy to be looking for it.]
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java