. Advertisement .
..3..
. Advertisement .
..4..
String parsing is the process of taking strings from a source (users, networks, or files) to extract information from them. Depending on your requirements, you can pick one of several ways to parse a string in Java. Keep reading to learn more about these methods.
Parse A String In Java
With split()
The method split() breaks a string into several parts based on a given rule. It returns an array containing strings that have been extracted from the original string. Its declaration:
public String[] split(String regex, int limit)
You will need to use a delimiting regular expression with this method, which then splits the string around matches created from the regular expression. The optional limit parameter determines the maximum number of substrings the method should return.
Example:
public class MyClass {
public static void main(String args[]) {
String str = "ITTutoria: Knowledge For Programmers";
String[] splitarray = str.split(": ");
for (String a : splitarray)
System.out.println(a);
}
}
Output:
ITTutoria
Knowledge For Programmers
Here we have a string with two components, one before the semicolon and one after. As the split() method is given “: ” as its argument, it splits the original string around the match and returns an array of strings.
This array contains two elements: “ITTutoria” and “Knowledge For Programmers”. We then use a for loop to go through this array and print every member to the console. Remember that you use the exact delimiter that separates two substrings, including white space characters.
Note: check out this guide if you want to know how to return a string in Java.
This is an example of using the method split()
to parse a phone number
public class MyClass {
public static void main(String args[]) {
String str = "425-555-0123";
String[] splitarray = str.split("-", 2);
for (String a : splitarray)
System.out.println(a);
}
}
Output:
425
555-0123
Since we have provided the limit argument, the method split() only returns an array of two strings. This means it only separates the original string into two parts, around the first occurrence of the delimiter.
With useDelimiter()
The method useDelimiter()
belongs to the Scanner class. By default, a Scanner in Java breaks the input into tokens using whitespaces. You can set this delimiting pattern with the method useDelimiter()
. Its declaration:
public Scanner useDelimiter(Pattern pattern)
// We can rewrite the first example and replace split() first useDelimiter():
import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner scan = new Scanner("ITTutoria: Knowledge For Programmers");
scan.useDelimiter(": ");
while(scan.hasNext()){
System.out.println(scan.next());
}
scan.close();
}
}
Output:
ITTutoria
Knowledge For Programmers
In the code above, the Scanner parses the original string into two tokens around the delimiter we provided. The hasNext()
method helps us to print them all out.
Remember that you can use a regular expression to set this delimiter.
Example:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner scan = new Scanner("google.com, yahoo.com, bing.com");
scan.useDelimiter("[,][ ]{0,1}");
while(scan.hasNext()){
System.out.println(scan.next());
}
scan.close();
}
}
Output:
google.com
yahoo.com
bing.com
The regular expression represents a comma and a space, which helps us separate two adjacent domains.
Conclusion
You can parse a string in Java with split()
or useDelimiter()
. The delimiting string can be set to a string or a regular expression for more complicated patterns.
Leave a comment