String API provides 2 methods to split String in java.
1 2 |
public String[] split(String regex) public String[] split(String regex,int limit) |
Both these methods return a array of String that has been split.
In this post we will see some examples using both these methods.
1. Split String using split(String regex)
In the below code we have used the split in 3 different scenarios
1. Splitting String with a delimiter as ” ” (space)
2. Splitting String with a delimiter as a character (“o” in case of the example)
3. Splitting String with a delimiter as a regular expression ([abc])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.kscodes.sampleproject; import java.util.Arrays; public class StringSplitExample { public static void main(String[] args) { // When " " is used as a delimiter String originalStr = "Please split me as i want to know how i look"; String afterSplitting[] = originalStr.split(" "); System.out.println("Splitting String with regex as a space :: " + Arrays.toString(afterSplitting)); // When used a character as a delimiter String originalStr1 = "How are you doing"; String afterSplitting1[] = originalStr1.split("o"); System.out.println("Splitting String with regex as 'o' :: " + Arrays.toString(afterSplitting1)); // When used a regular expression as delimiter String originalStr2 = "Please split me as i want to know how i look"; String afterSplitting2[] = originalStr2.split("[abc]"); System.out.println("Splitting String with regular expression as '[abc]' :: " + Arrays.toString(afterSplitting2)); } } |
Output
123
Splitting String with regex as a space :: [Please, split, me, as, i, want, to, know, how, i, look]Splitting String with regex as 'o' :: [H, w are y, u d, ing]Splitting String with regular expression as '[abc]' :: [Ple, se split me , s i w, nt to know how i look]
2. Split String using split(String regex,int limit)
1 2 3 |
Splitting String with regex as a space :: [Please, split, me, as, i, want, to, know, how, i, look] Splitting String with regex as 'o' :: [H, w are y, u d, ing] Splitting String with regular expression as '[abc]' :: [Ple, se split me , s i w, nt to know how i look] |
2. Split String using split(String regex,int limit)
For splitting a String you can also specify how much a string should be split.
split(String regex,int limit) works similar to split(String regex), but stops splitting the string once the given limit is achieved.
We will use the same example that we have discussed above, only difference : we will be adding a limit to split.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.kscodes.sampleproject; import java.util.Arrays; public class StringSplitExample { public static void main(String[] args) { // When " " is used as a delimiter String originalStr = "Please split me as i want to know how i look"; String afterSplitting[] = originalStr.split(" ",4); System.out.println("Splitting String with regex as a space :: " + Arrays.toString(afterSplitting)); // When used a character as a delimiter String originalStr1 = "How are you doing"; String afterSplitting1[] = originalStr1.split("o",1); System.out.println("Splitting String with regex as 'o' :: " + Arrays.toString(afterSplitting1)); // When used a regular expression as delimiter String originalStr2 = "Please split me as i want to know how i look"; String afterSplitting2[] = originalStr2.split("[abc]",5000); System.out.println("Splitting String with regular expression as '[abc]' :: " + Arrays.toString(afterSplitting2)); } } |
We have added 3 types of limit.
1. In 1st case we have added 4 as the limit. Here the output is – String is split in 4 parts
2. In 2nd case we have added 0 as the limit. Here the output is – String is split similar to Split(String regex)
So we should note that, if we want to split a string for desired limit, the the limit should be greater than 0.
3. In 3rd case we have added a very large limit. Here the output is – String is split similar to Split(String regex).
Output
1 2 3 |
Splitting String with regex as a space :: [Please, split, me, as i want to know how i look] Splitting String with regex as 'o' :: [How are you doing] Splitting String with regular expression as '[abc]' :: [Ple, se split me , s i w, nt to know how i look] |