Java - Split String Using Regex
Last Updated :
09 Dec, 2024
In Java, the split() method breaks a String into parts based on a regular expression (regex). This allows us to split the string using complex rules, such as splitting at any digit, word boundary, or special character.
Example 1: Here, we are using the most simple split() method with the regex \\d+, which splits the string wherever one or more digits appear.
Java
// Java program to split a string by digits
public class SplitString{
public static void main(String[] args) {
String s = "apple123banana456cherry";
// Split by one or more digits
String[] p = s.split("\\d+");
for (String part : p) {
System.out.println(part);
}
}
}
Outputapple
banana
cherry
This is helpful when we want to separate alphabetic words from numbers in a string.
Example 2: We can also split a string by multiple delimiters, such as spaces or commas. The regex [,\s]+ matches one or more spaces or commas.
Java
// Java program to split a string
// by spaces or commas
public class SplitStringByRegex {
public static void main(String[] args) {
String s = "apple,banana orange,cherry";
// Split by commas or spaces
String[] p = s.split("[,\\s]+");
for (String part : p) {
System.out.println(part);
}
}
}
Outputapple
banana
orange
cherry
This is useful when parsing data with multiple delimiters, like in CSV or spaced-separated values.
Example 3: Here, we use the Pattern.split() method to split the string using a regex pattern. This provides more flexibility for advanced matching conditions.
Java
// Java program to split string using Pattern.split()
import java.util.regex.Pattern;
public class SplitString {
public static void main(String[] args) {
String s = "apple123banana456cherry";
// Regex for one or more digits
Pattern p = Pattern.compile("\\d+");
// Split using Pattern class
String[] parts = p.split(s);
for (String part : parts) {
System.out.println(part);
}
}
}
Outputapple
banana
cherry
Example 4: Here, we use Java Streams to split the string and print the parts in a more functional programming style. This is a modern approach for splitting strings and processing the results.
Java
// Java program to split string using Streams
import java.util.Arrays;
public class SplitString {
public static void main(String[] args) {
String s = "apple123banana456cherry";
// Split by one or more digits and
// convert to stream
Arrays.stream(s.split("\\d+"))
.forEach(System.out::println);
}
}
Outputapple
banana
cherry
When to Use Which Method
- split() is the simplest way to split a string using a regex.
- Pattern.split() provides more advanced regex matching and flexibility.
- Java Streams allow for a functional approach to processing strings after splitting.
Similar Reads
Java String split() Method The String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.Example: Here, we will split a String having multiple delimiters or re
6 min read
Java String substring() Method In Java, the substring() method of the String class returns a substring from the given string. This method is most useful when you deal with text manipulation, parsing, or data extraction.This method either takes 1 parameter or 2 parameters, i.e., start and end value as arguments.In case the end par
2 min read
Removing whitespaces using Regex in Java Given a string, your task is to remove the whitespaces in the string using Java Regex (Regular Expressions). Examples Input : Hello Everyone . Output : HelloEveryone. Input : Geeks for Geeks . Output : GeeksforGeeks.Regular Expressions Regular Expressions or Regex is an API for defining String patte
3 min read
Extracting each word from a String using Regex in Java Given a string, extract words from it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Funny?? are not you? Output : Funny are not you Input : Geeks for geeks?? Output : Geeks for geeks We have discussed a solut
2 min read
Print first letter of each word in a string using regex Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeksOutput :Gfg Input : United KingdomOutput : UKBelow is the Regular expression to extract
3 min read
Splitter trimResults() method | Guava | Java The method trimResults() returns a splitter that behaves equivalently to this splitter, but automatically removes leading and trailing whitespace from each returned substring. For example, Splitter.on(', ').trimResults().split(" a, b, c ") returns an iterable containing ["a", "b", "c"]. Syntax: publ
2 min read