Confused how .split () works in Java

I have this line which I take from a text file.

"1 normal 1 [(o, 21) (o, 17) (t, 3)]" 

I want to take 1, normal, 1, o, 21, 17, t, 3 into an array of strings.

 Scanner inFile = new Scanner(new File("input.txt"); String input = inFile.nextLine(); String[] tokens = input.split(" |\\(|\\)|\\[\\(|\\, |\\]| \\("); for(int i =0 ; i<tokens.length; ++i) { System.out.println(tokens[i]); } 

Output:

 1 normal 1 o 21 o 17 t 3 

Why spaces are stored in the array.

+5
source share
2 answers

These are not spaces, these are empty lines. Your line:

 "1 normal 1 [(o, 21) (o, 17) (t, 3)]" 

It is broken as follows according to your regular expression:

 Token = "1" Delimiter = " " Token = "normal" Delimiter = " " Token = "1" Delimiter = " " Token = "" <-- empty string Delimiter = "[(" Token = "o" ... end so on 

When two adjacent delimiters appear, he believes that there is an empty token between them.

To fix this, you can change your regular expression, for example, as follows:

 "[ \\(\\)\\[\\,\\]]+" 

Thus, any number of " ()[,]" adjacent characters will be considered a delimiter.

+6
source

For instance:

 1 [(o 
  • In the first step, it corresponds to a single space.
  • The next step corresponds to [(

Thus, between these two matches, void String "" is returned.

0
source

Source: https://habr.com/ru/post/1233589/


All Articles