When I split the string "1|2|3|4"with String.split("|"), I get 8 elements in the array instead of 4. If I use "\\|", the result will be correct. I guess this is something to do with regular expressions. Can anyone explain this?
"1|2|3|4"
String.split("|")
"\\|"
You're right. |- a special character for alternation. A regular expression |means "empty string or empty string". Thus, it will be divided into all empty lines, resulting in 1 element for each character in the line. Shielding \|makes it normal.
|
\|
, Splitter Guava. , ..
Iterable<String> split = Splitter.on('|').split("1|2|3|4");
|is ORin the Java regular expression syntax, basically splitting 1|2|3|4by |equal value String#split()to “split this line between blank OR blank”), which means that it is split after every character that you are in the original string.
OR
1|2|3|4
String#split()
Source: https://habr.com/ru/post/1766471/More articles:How to check classes in a hierarchy in a generic type in a safe way? - inheritanceMath me - 2d video games - mathKohana development cycle - phpHow to change keyboard type for text field in Xcode - objective-cHow can I pack a scrapy project using cxfreeze? - pythonОбработка кликов для jQuery AJAX (с PHP) - jqueryInserting nodes into a linked list - javaGIT: является уникальным тегом для фиксации? - gitAJAX toolkit - problem with webservice execution - javascriptError accessing a Windows PGM socket with an account without administration - c #All Articles