| not recognized in java string.split ()

I am having problems with java string.split method.

I have a string word that is - freshness|originality. Then I split this line as follows:

   String words[] = word.split("|");

If I then deduce the words [1], like this:

    t1.setText(words[1]); 

This gives me a f value. I found out that this is the word f in the word freshness.

How can I split the string correctly so that the words [1] are actually originality? Thanks for the help!

+4
source share
4 answers

You should avoid this:

String words[] = word.split("\\|");

Note this explanation in a similar question here: Why does String.split require a string separator to be escaped?

String split() . , unescaped | , OR " ".

+9

, Java .

line.split("\\|")

"|" gets " ", , .

...?, + ^: - $*

- .

+3

. "\\|".

Read more about regex escaped characters here .

+2
source
        String test ="freshness|originality";
        String[] splits = test.split("\\|");
        String part1 = splits[0]; // freshness
        String part2 = splits[1]; // originality
0
source

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


All Articles