Open In App

Java - Split String Using Regex

Last Updated : 09 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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);  
        }
    }
}

Output
apple
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);  
        }
    }
}

Output
apple
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); 
        }
    }
}

Output
apple
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);  
    }
}

Output
apple
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.

Article Tags :
Practice Tags :

Similar Reads