my code is:-
class SplitString {
public static void main(String[] args) {
Pattern p;
String test = "a1b2c3";
String[] token1 = test.split("\\d");
System.out.println("first case : " + token1.length);
for (String s : token1)
System.out.print(s + " ");
String[] token2 = test.split("\\b");
System.out.println("\n\nsecond case : " + token2.length);
for (String s : token2)
System.out.print(s + " ");
String[] token3 = test.split("\\a");
System.out.println("\n\nthird case : " + token3.length);
for (String s : token3)
System.out.print(s + " ");
}
}
Output:-
first case : 3
a b c
second case : 2
a1b2c3
third case : 1
a1b2c3
I am new to java and trying to execute split but cannot able to grasp its concept since all cases have different answers but what exactly the difference between them?
The
splitmethod, as you may know, uses a regex pattern to split the string:\dsplits it using number digits as delimiters\bsplits it using word boundaries, so you actually have an empty string and the whole remainder\ais a special character, which is not present in the string you are splitting, so you just have one tokenTake a look here for all the regex options.