String parsing in Java means extracting useful information from a given string based on specific patterns/requirements. Java refers to this extracted information as tokens. Developers use string parsing in scenarios where a string contains detailed information, but only specific information is needed. For example, extracting an employee’s experience from a resume, extracting numbers from a string, etc.
This Java guide will illustrate how a string can be parsed using different built-in classes and methods.
How to Parse a String in Java?
Java offers different approaches to parse a string, such as the “parse()” method, the “split()” method, the “useDelimiter()” method, “regular expressions”, the “StringUtils” class, and the “StringTokenizer” Class.
Method 1: Parse String Using parse() Method
parse() is a built-in method of the Java Period class that accepts a string as an argument and parses it according to a specific format, as demonstrated in the following syntax:
public static Period parse(CharSequence str);
Here, “str” represents a string to be parsed which must be in “PnYnMnD” format. Where, “P” indicates a period, “nY”, “nM”, and “nD” denote “year”, “month”, and “day”, respectively.
Example: Suppose we have an employee named “Joseph” and we want to compute his experience in terms of years, months, and days. To do this, first, we import the “Period” class:
import java.time.Period;
Next, in the main() method, we initialize a string to be parsed in the “PnYnMnD” format:
String experience = "P03Y06M12D";
After that, we create an object of the Period class and pass the targeted string to the parse() method. As a result, the provided string will be parsed into a period:
Period obj = Period.parse(experience);
Then, we use the getYears(), getMonths(), and getDays() methods of the Period class to retrieve the experience in years, months, and days, respectively. We wrap these methods within the “println()” method to display the result on the console:
System.out.println("Joseph's Total Experience is: \n" +
obj.getYears() + " Years\n"
+ obj.getMonths() + " Months\n"
+ obj.getDays() + " Days\n"
);
Complete Code & Output
Method 2: Parse String Using split() Method
Java’s String class offers a built-in split() method that splits/parses the given string according to the specified delimiter. It retrieves a new array of substrings without impacting the original/actual array. The split() method has two variants that are illustrated below:
//First Variant
string.split(regex/delimiter);
//Second Variant
string.split(regex/delimiter, limit);
If the split() method is executed with the “limit” parameter, it splits the given string according to the specified limit.
Example 1: Suppose we are given a date as a string in “YYYY/MM/DD” format. The task is to parse the given string from the “/” symbol using the split() method. The given string is:
String date = "2023/12/18";
We use the “split()” method and pass the “/” symbol to it as an argument. The split() method will traverse the given string from left to right and parse/split it where the “/” symbol appears:
String[] splitDate = date.split("/");
After that, we use the for loop to iterate the parsed string. Also, we invoke the println() method to display the parsed string on the console:
System.out.println("The parsed string: ");
for (String dat: splitDate) {
System.out.println(dat);
}
Complete Code & Output
Example 2: Let’s proceed one step ahead to learn how the split() method can be used in Java with two arguments. We will apply the split() method on the same string with “limit” restrictions. For this, first, we initialize a string:
String date = "2023/12/18";
Next, we apply the split() method on the given string. But this time, we specify the limit as 2. As a result, the targeted string will be split/parsed into two parts:
String[] splitDate = date.split("/", 2);
Finally, print the parsed string:
System.out.println("The parsed string: ");
for (String dat: splitDate) {
System.out.println(dat);
}
Complete Code & Output
This time the split() method splits the string into two parts (i.e., specified limit) irrespective of how many times the “/” symbol (i.e., delimiter) appears in the given string.
Method 3: Parse String Using useDelimiter() Method
The useDelimiter() method belongs to Java’s Scanner class that parses the given string based on the specified patterns. This pattern can be a simple string or a special character depending on the user’s requirements/preferences. This method is executed with the Scanner.next() and Scanner.hasNext() methods to iterate and retrieve the next tokenized string according to the specified pattern.
public Scanner useDelimiter(Pattern | String pattern);
Example: In this code, we will apply the useDelimiter() method of Java’s Scanner class to parse the given string according to the specified delimiter. For this purpose, first, we import the “Scanner” class:
import java.util.Scanner;
Now, we declare a variable and initialize it with a string that needs to be parsed:
String date = "2023/12/18";
After that, we create an object of the Scanner class using the Scanner() constructor and pass the desired string to it as an argument:
Scanner obj = new Scanner(date);
After this, we use this object with the useDelimiter() method to parse the string. The useDelimiter() function accepts the “/” symbol as an argument to parse the string accordingly:
obj.useDelimiter("/");
After this, we execute the “while” loop with the help of hasNext() and next() methods to print the parsed/ tokenized strings:
System.out.println("The parsed string: ");
while (obj.hasNext()){
System.out.println(obj.next());
}
Finally, we close the scanner object using the close() method:
obj.close();
Complete Code & Output
Method 4: Parse String Using StringTokenizer Class
The StringTokenizer class came under the umbrella of the “java.util” package and can be used to parse the given strings. To parse a string using the StringTokenizer class, pass the targeted string and a delimiter as an argument to the StringTokenizer(), as shown in the following syntax:
StringTokenizer obj = new StringTokenizer(str, del);
Here, “str” represents a string to be parsed and del represents a “delimiter” based on which the given string will be parsed.
Example: This example implements the StringTokenizer() constructor, the hasMoreTokens() method, and the nextToken() method to parse the given string. To do that, first, we import the StringTokenizer class:
import java.util.StringTokenizer;
After this, in the main() method, we create a string and store it in the variable “date”:
String date = "2023/12/18";
Next, we create an object of the stringTokenizer class using its constructor. We pass the declared string and a delimiter to the constructor as its arguments:
StringTokenizer obj = new StringTokenizer(date, "/");
Finally, we use the while loop with the hasMoreTokens() and nextToken() methods to check if the string has more tokes, if yes, then print them:
System.out.println("The parsed string: ");
while (obj.hasMoreTokens()){
System.out.println(obj.nextToken());
}
Complete Code & Output
Method 5: Parse String Using Regex
Java doesn’t offer a built-in “regex” class, however, it provides a “regex” package that contains Pattern, Matcher, and PatternSyntaxException classes. These classes can be used to define regex patterns for parsing a Java string.
Example: This example will utilize the regex patterns to parse a string. For this purpose, first, we import the “java.util.regex” package:
import java.util.regex.*;
After this, we initialize a string in the main() method:
String date = "2023/12/18";
Next, we specify a regex pattern to match the “YYYY/MM/DD” format:
String regex = "(\\d{4})/(\\d{2})/(\\d{2})";
Then we utilize the Pattern.compile() method and pass it the regex pattern. As a result, the stated method will compile the targeted regex and retrieve the instance of the Pattern:
Pattern = Pattern.compile(regex);
Also, we execute the matcher() method to match the given string with the regex pattern:
Matcher matchPattern = pattern.matcher(date);
Finally, we execute the while loop with the matchPattern.find() method to traverse the input string and find the desired matches. The matchPattern.group() method is used within the println() method to extract and print each match:
while (matchPattern.find()) {
System.out.println("The Year is " + matchPattern.group(1));
System.out.println("The Month is " + matchPattern.group(2));
System.out.println("The Day is " + matchPattern.group(3)); }
Complete Code & Output
Method 6: Parse String Using StringUtils Class
In Java, the StringUtils class can be used to parse strings. It is a utility class that belongs to a third-party library named “Apache Commons Lang”. It offers some advanced string methods that are not present in Java’s standard String class. To use this class, users must create a Maven or Gradle project and add the Apache Commons Lang dependency in the “pom.xml” or “build.gradle” file, respectively.
Example: We create a new Maven project and add the following dependencies in its “pom.xml” file”:
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
After adding the dependencies, we are all set to import the StringUtils class:
import org.apache.commons.lang3.StringUtils;
After this, we initialize a string to be parsed to a variable named greetings:
String greetings = "Welcome to JavaBeat.net - The Best Site for Programmers";
We use the substringsBetween() method with three arguments: “greetings”, “to”, and “-”. The “greetings” represents a string to be parsed, while “to ”, and “-” are starting and ending delimiters. This means the given string will be parsed from the “to ” to the “–”:
String[] parseString = StringUtils.substringsBetween(greetings, "to ", "-");
Finally, we use the println() method within the for loop to print the parsed string:
for (String i : parseString) {
System.out.println("The Parsed String is: " + i);
}
Complete Code & Output
Useful Parse() Methods in Java
Java offers several parse() methods that belong to different built-in Java classes. Each of them has the same ultimate objective, i.e., parsing the given string, however, the implementation of each method varies according to the respective class.
The following table illustrates different parse() methods, the class name to which they belong, syntax, and their respective description:
Method | Class | Syntax | Description |
---|---|---|---|
parseInt() | Integer | parseInt(String str);ORparseInt(String str, int radix); | Converts the given string to an integer. |
parseDouble() | Double | parseDouble(String str); | Converts the given string to a double value. |
parse() | Period | parse(CharSequence str); | Parses the strings in “PnYnMnD” format. |
parse() | SimpleDateFormat | parse(String str, ParsePosition start_index); | Parses the dates in a locale-sensitive format. |
parse() | LocalDateTime | parse(CharSequence str); | Accepts a valid DateTime and parses it to a local DateTime. |
parse() | LocalDate | parse(CharSequence str); | Accepts a valid Date and parses it to a local date. |
parse() | LocalTime | parse(CharSequence str); | Accepts a valid Time value and converts it to a local time. |
parse() | ZonedDateTime | parse(CharSequence str);ORparse(CharSequence str, DateFormatter formatter); | Accepts a DateTime string and parses it to ZonedDateTime. |
parse() | MessageFormat | parse(String str, ParsePosition start_index); | Accepts a string and retrieves an array of parsed objects. |
parse() | NumberFormat | parse(String str); | It parses a string to a number. |
parse() | Instant | parse(CharSequence str); | Parses the given string and retrieves an instance of Instant in the UTC zone. |
parse() | Level | parse(String str); | It accepts a string, parses it, and returns a Level object. |
This is how you can parse the strings in Java.
Final Thoughts
In Java, users can parse a string using several built-in methods, including “parse()”, “split()”, and “useDelimiter()”. Moreover, the “StringUtils” class enables Java users to retrieve a particular part of a string from a specific location. Users can also use “regular expressions” to parse the strings based on the required pattern.
Among all these approaches, the split() method is the center of attention for every user, as it allows adding delimiter, regex, and limit constraints. This extensive guide has elaborated on all these approaches with suitable examples.