Given: 11. String test= “a1b2c3”; 12. String[] tokens = test.split(”\d”); 13. for(String s: tokens) System.out.print(s +“ “); What is the result?
Reveal answer
Fill a bubble to check yourself
Given: 11. String test= “a1b2c3”; 12. String[] tokens = test.split(”\d”); 13. for(String s: tokens) System.out.print(s +“ “); What is the result?
a b c
1 2 3
a1b2c3
Compilation fails
The regex \d matches each digit character. String.split() removes these digit delimiters, producing an array [a, b, c]. The enhanced for loop prints each token followed by a space, resulting in a b c. Trailing empty strings are discarded by default in Java's split method.
To determine the result of the given code, let's go through each line:
String test = "a1b2c3"; - This line declares and initializes a String variable named "test" with the value "a1b2c3".
String[] tokens = test.split("\d"); - This line splits the string "test" using the regular expression "\d", which matches any digit. The result is an array of strings, where each element contains the substrings separated by digits. In this case, the array will contain ["a", "b", "c"].
for(String s : tokens) System.out.print(s + " "); - This line iterates over each element in the "tokens" array and prints it followed by a space. The output will be "a b c".
Therefore, the result of the given code is:
A) a b c