How to Split Strings in Java
Using StringTokenizer : The StringTokenizer is from the package java.util.StringTokenizer and the code snippet is as follows:StringTokenizer st =new StringTokenizer("string tokenizer example"); System.out.println("tokens count: " +...
Step-by-Step Guide
-
Step 1: Using StringTokenizer : The StringTokenizer is from the package java.util.StringTokenizer and the code snippet is as follows:StringTokenizer st =new StringTokenizer("string tokenizer example"); System.out.println("tokens count: " + st.countTokens()); // iterate through st object to get more tokens from it while (st.hasMoreElements()) { String token = st.nextElement().toString(); System.out.println("token = " + token); } The result of the above code is tokens count: 3 token = string token = tokenizer token = example;
(1) String[] split( String regEx) which splits the string according to given regular expression. (2) String[] split( String regEx, int limit ), which splits the string according to given regular expression.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array Code snippet for String[] split( String regEx)String str = "st1-st2-st3"; String delimiter = "-"; String[] temp; temp = str.split(delimiter); for(int i =0; i < temp.length ; i++) System.out.println(temp); The output is: st1 st2 st3 Code snippet for String[] split( String regEx, int limit )String str = "st1-st2-st3"; String delimiter = "-"; String[] temp; temp = str.split(delimiter, 2); for(int i =0; i < temp.length ; i++) System.out.println(temp); The output is st1 st2-st3 , The output of the above snippet is -
Step 2: Using split method: Java String class defines two split methods to split Java String object.
-
Step 3: Using regular expression: An example for this is as followsString input = "st1:st2:st3"; System.out.println(Arrays .asList(Pattern.compile(":").split(input))); System.out.println(Arrays.asList(Pattern.compile(":").split(input
-
Step 4: 2))); Here
-
Step 5: the second sysout is the example of the split method with the limit argument.
Detailed Guide
(1) String[] split( String regEx) which splits the string according to given regular expression. (2) String[] split( String regEx, int limit ), which splits the string according to given regular expression.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array Code snippet for String[] split( String regEx)String str = "st1-st2-st3"; String delimiter = "-"; String[] temp; temp = str.split(delimiter); for(int i =0; i < temp.length ; i++) System.out.println(temp); The output is: st1 st2 st3 Code snippet for String[] split( String regEx, int limit )String str = "st1-st2-st3"; String delimiter = "-"; String[] temp; temp = str.split(delimiter, 2); for(int i =0; i < temp.length ; i++) System.out.println(temp); The output is st1 st2-st3 , The output of the above snippet is
About the Author
Daniel Green
With a background in manufacturing, Daniel Green brings 15 years of hands-on experience to every article. Daniel believes in making complex topics accessible to everyone.
Rate This Guide
How helpful was this guide? Click to rate: