Scenario : Suppose we have a String separated by pipeline ( | ). For example consider,
Given String : Learn|Java|Like|Never|Before
Requirement : Break the string at |
Expected Output : Learn
Java
Like
Never
Before
Solution : There are different ways of doing it. Lets look at each of them one by one.
1) Using StringTokenizer : If you want to use this approach then first thing you need to do is to import java.util.StringTokenizer. Lets have a look at the below screenshot.
split String at delimiter using StringTokenizer |
Few things that we need to discuss here is:
- StringTokenizer s1_token = new StringTokenizer(s1, "|"); Here we pass the String object s1 (which needs to be broken) and then delimiter | (where it needs to be broken) into the StringBuilder. And it breaks the original string Learn|Java|Like|Never|Before at the provided delimiter into small tokens of Learn, Java, Like, Never, Before. These tokens are stored in variable s1_token.
- s1_token.hasMoreTokens() This helps in checking if more tokens are present or not. While loop executes till tokens are present in s1_token.
- s1_token.nextToken() This fetches the stored tokens from s1_token starting from first to last, one by one and each time the value is stored in String s2 and printed as shown in the output.
2) Using split() method : Use of split() method is generally preferred over StringTokenizer() to split the string. Reason being StringTokenizer() is a legacy method and is generally kept for compatibility reason.
Split String at the delimiter using split() |
Few things that needs to be discussed here is :
- split() - split(String regex) splits the String around matches of the given regular expression. And the result will be stored in String array.
Another version of this method is split(String regex, int limit) - use of "\\" in split() - In previous example we just used | and the task was done. Then why use \\? Nice question. If you are asking this question then you are surely trying to learn. So the answer of your question is split method takes regular expression as parameter and hence to break the string on | (or any other special character) remember to use \ to escape individual special character.
Suppose you have Hello^World and you want to break at ^ using split. So use split("\\^") - //String[] s2 = s1.split(Pattern.quote("|")); - You can also use this one instead of String[] tokens = s1.split("\\|"); for the purpose. If you are using this one then don't forget to import java.util.regex.Pattern, otherwise you will get compile time error.
Note : Don't just go through the code rather code yourself and experiment to understand better. If you have query feel free to ask.
You might also want to know about:
- package
- import
- different String methods (like tokens.length used in above example)
- Regular Expression
- Difference between StringBuffer and StringBuilder
- Why String is considered immutable in java?
No comments:
Post a Comment