After we split(" ") the string what is the value of array[4](the last element of array) if the string to split is "3 + 6 = "(4 char and 4 char space)? We have " "(space) after = but if we split with regex " " and after the space we don't have nothing why the array have 5 elements!?
How regex work?
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa
19 February 2022, 10:10
Without your code and the testcases I just can guess what happens and I assume you use a string containing two consecutive spaces (Gellert 👍).
Have a look at the docs. They tell that split taking one argument calls split(" ", 0);
Here you can read smth about 'positive match at the beginning' and 'an empty leading substring is included at the beginning of the resulting array'. Means if you're splitting using a space and a space is at the beginning of your string, then an empty string element is added to the array.
If you have two spaces somewhere inside your string then this is handled similar cause after each match the split method looks at the resulting string in a new iteration (as if it were a new string -- it is using substring and offsets though).
You can get around those problems if you use greedy regex like \\s+ that'll greedy look for spaces (one or more, till it finds another char) and splits using all found as delimiter. That would take care of multiple inner spaces. To get rid of leading spaces just use trim()...
0
Gellert Varga
19 February 2022, 19:58
Thanx, I learned something new again.:)
+1
Angel Stefan
21 February 2022, 19:21
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) {
PrintStream pr = System.out;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteArrayOutputStream);
System.setOut(printStream);
testString.printSomething();
String x = byteArrayOutputStream.toString();
System.setOut(pr);
System.out.println(x); //"3 + 6 = "
String[] strings = x.split(" ");
System.out.println(strings.length); // 5
int a = Integer.parseInt(strings[0]);
String math = strings[1];
int b = Integer.parseInt(strings[2]);
int c = 0;
switch (math){
case "+": {
c = a + b;
break;
}
case "-": {
c = a - b;
break;
}
case "*":{
c = a * b;
break;
}
}
strings[4]= String.valueOf(c);
for(String s : strings){
System.out.println(s +" ");
}
}
public static class TestString {
public void printSomething() {
System.out.println("3 + 6 = ");
}
}
}
0
Lisa
21 February 2022, 20:19useful
you have 5 elements in the array as expected (cause you split using " ")
the initial string was smth like that "3 + 6 = \n" -> result 3, +, 6, =, \n (don't forget that the output was done using println())
you can get rid of the \n using substring or you use "\\s+" as I mentioned above or (untested) " |\\p{Cc}"
you can see the linebreak easily when you output x as ascii code
+1
Angel Stefan
23 February 2022, 11:16
Ok, i understand, i have five elements because at the end of the string i have char '\n'(10 in ASCII). Tk you!
0
Gellert Varga
19 February 2022, 09:42
Please share your whole code about your question.
I tried this code:
and the output is:
[3, +, 2, =]
The length of array: 4 0