Parse a String in Java

  1. Use the split Method to Parse a String in Java
  2. Use Scanner to Parse a String in Java
  3. Use StringUtils to Parse a String in Java

This tutorial explains how to parse a string in Java using various methods. Parsing is the process of taking a string and processing it to extract information.

Use the split Method to Parse a String in Java

The split() method of the String class works by splitting the source string keeping the original string unmodified, and returns an array of substrings of the original string. This method has two variants.

The split(String regex) method takes a regular expression of type string as an argument and splits the string around the regular expression’s matches. If the regular expression fails to match any part of the original string, it returns an array with one element: the source string.

The split(String regex, int limit) method works the same but takes limit, which means how many strings to be returned. If the limit is negative, the returned array can contain as many substrings as possible when the limit is 0. The array would contain all substrings, excluding the trailing empty strings.

public class StringTest {
    public static void main(String args []){
        String source1 = "March032021";
        String [] returnedArray1 = source1.split("\\d+");
        for(String str1 :  returnedArray1){
            System.out.println(" Output1 : "+str1);
        }
        String source2 = "950-003-123-900-456 : 11 _343-1 789----";
        String [] returnedArray2 = source2.split("-",4);
        for(String str2 :  returnedArray2){
            System.out.println(" Output2 : "+str2);
        }

    }
}

Output:

Output1 : March
 Output2 : 705
 Output2 : 103
 Output2 : 102
 Output2 : 456-123 : 112 _343-1 789----

Use Scanner to Parse a String in Java

Scanner is generally used to parse primitive types and strings using a regular expression. It breaks the input into tokens using a delimiter pattern which be default matched white-space.

We create a scanner with a specified string object. The useDelimiter() method of the Scanner class is used to set the delimiter pattern. We can either pass a Pattern object or string as a pattern. To get all the tokens of the string, we loop through the tokens using the hasNext() method and print the output.

import java.util.Scanner;
public class StringTest {
    public static void main (String args[]){
        String text = "John Evans was born on 25-08-1980";
        Scanner scanner = new Scanner(text);
        scanner.useDelimiter("born");
        while (scanner.hasNext()){
            System.out.println("Output is : "+scanner.next());
        }
    }
}

Output:

Output is : John Evans was 
Output is :  on 25-08-1980

Use StringUtils to Parse a String in Java

Apache Commons StringUtils class provides tools that facilitate easy working with Strings. The maven dependency to add this library is given below.

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>

We use the substringBetween(String str, String open, String close) method of the StringUtils class to parse a given string. This method extracts a substring nested between two strings.

import org.apache.commons.lang3.StringUtils;

public class StringTest {
    public static void main(String args[]) {
        String source = "The crazy brown dog jumps over the fence";
        String[] stringArray = StringUtils.substringsBetween(source, "crazy", "over");
        for (String s : stringArray) {
            System.out.println("Output: " + s);
        }
    }
}

Output:

Output:  brown dog jumps 
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java String

  • Replace Character in String in Java
  • Perform String to String Array Conversion in Java