Error in exit using split () method?

I am trying to use a simple split () method, but the output I get is not correct. I am using this code:

question = newobject.ACTIVITY_LIST_OF_QUESTIONS.split("|"); 

where newobject.ACTIVITY_LIST_OF_QUESTIONS contains 1 | 2 | 8 | 11 | 4 | 5 | 6 | 14 | 15 | 16 | 13 | 17 | 7 | 9 | 12 | 10 is like String, so I have to get every number in the index of the array.

But instead, I get the output -

  1 | 2 | 8 

Help if someone had the same problem?

+4
source share
3 answers

You need to go out | like \\| . Your regular expression is interpreted as "empty string or empty string", so each position corresponds.

+1
source

You must use split("\\|") . You need to break the special meaning of regex | . You do this with \\| . [Note that split() splits according to regex].

 String s = "1|2|8|11|4|5|6|14|15|16|13|17|7|9|12|10"; String[] arr = s.split("\\|"); System.out.println(Arrays.toString(arr)); 

leads to:

 [1, 2, 8, 11, 4, 5, 6, 14, 15, 16, 13, 17, 7, 9, 12, 10] 
+3
source

A | - The regular expression metacharacter used to indicate a change. To indicate a literal | you need to avoid it as \\| or put it in the character class [|] .

+1
source

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


All Articles