Split Reasons for the split by a single character

First, the problem Description

In use the String.split () when the string note resolution, if used as a special delimiter symbols, can not be properly resolved. E.g:

1 String info = "org|shop|person";
2 String [] array= info.split("|");
3 
4 System.out.println("array[0] --- "+array[0]);
5 System.out.println("array[1] --- "+array[1]);
6 System.out.println("array[2] --- "+array[2]);

Operating results :

  array[0] --- o
  array[1] --- r
  array[2] --- g

 You will find that the results are not as expected org, shop, person three strings, but is performed by splitting a single character.

Second, the cause

split () supported by a regular expression to split, as has been the "|" delimiter to do, be mistaken for a regular expression.

Third, treatment options

Processing method is very simple, before adding an escape character text delimiter "\\" to.
1 String info = "org|shop|person";
2 String [] array= info.split("\\|");
3 
4 System.out.println("array[0] --- "+array[0]);
5 System.out.println("array[1] --- "+array[1]);
6 System.out.println("array[2] --- "+array[2]);
Operating results :

  array[0] --- org
  array[1] --- shop
  array[2] --- person

 

Similarly there is a "." Symbol for the division of the situation.

 

Guess you like

Origin www.cnblogs.com/ayrie/p/12482238.html