This blog will explain how to parse a string in Java. Let’s start!
Parse a String in Java
In Java, there exist three main approaches to parse a string:
- Parse string by using Java Split() method
- Parse string by using Java Scanner class
- Parse string by using StringUtils class
We will now discuss each of the above-mentioned approaches in detail.
Method 1: Parse String by Using Java split() Method
In Java, there is a split() method of the String class that splits the given string and returns the array of substrings. It keeps the given string unchanged. Also, the split() method is case-sensitive.
The split() method has two variations.
- split(regular-expression/delimiter, limit)
Have a look at the given examples to know more about the usage of the split() method.
Example 1: Parsing String in Java Using split(regular-expression/delimiter) variant
In this example, we will use the split(regular-expression/delimiter) variant of the split() method. A regular expression is passed as an argument for this method. If the given expression matches the string, it will divide the string; otherwise, it will print out the whole string.
Here, we have a string named stg:
While using the split() method, you can also use different delimiters as a condition, such as any alphabet from string or special character, and many more. The below-given string will be split based on the white spaces:
Lastly, for printing the parsed string, use for loop:
As you can see, the split() method has successfully parsed the given string based on the occurrence of the white spaces:
Example 2: Parsing String in Java Using split(regular-expression/delimiter, limit) variant
This variant works almost the same as the above. Here, we will add the limit with a delimiter, which determines the number of splitting strings according to the string length.
For instance, we have a string named stg:
We will use for loop to split the whole stg string with white space as delimiter and limit as 3:
The specified string will split as the space occurs, and it will return three strings according to the added limit:
Method 2: Parse String by Using Java Scanner Class
To parse a string, Java Scanner class commonly uses a regular expression or regex. It divides the given string into tokens by using a useDelimiter() method.
First, we have a string stng that needs to be parsed:
Create an object of the Scanner class and pass the string stng as a parameter:
The delimiter pattern is set using the useDelimiter() method of the Scanner class. Here, we will pass the colon “:” as a delimiter pattern in the useDelimiter() method:
This method splits the string when it finds a colon. To obtain all of the tokens in the string, use the hasNext() method in a while loop and print the result:
Method 3: Parse String by Using StringUtils Class
In order to parse a string using StringUtils class, first of all, we will create a maven project rather than a simple Java project and then add dependencies to it.
Here, we have specified maven dependency for adding StringUtils library in our XML file:
Then, create a Java file, and use the StringUtils class to parse the string stng:
We will use the substringsBetween() method of StringUtils class, specify the stng string and pass “:” and “!” as delimiters which means that the resultant value will contain the substring that is present in between these two delimiters:
To print the parsed strings, utilize for loop:
The output will display the substring between “:” and “!” delimiters:
We have provided the information related to parsing a string in Java.
To parse a string in Java, you can use the Java String split() method, Java Scanner class, or StringUtils class. For parsing a string based on the specified condition, these methods use delimiters to split the string. However, the split() method is majorly utilized as it supports adding delimiter/ regex and the relative limit. This blog explained the methods to parse a string in Java with examples.