Calling Java method String split ("|") not working correctly

public class SplitStr {

    public static void main(String []args) {
        String str1 = "This | is | My | Account | For | Java|";
        String str2 = "This / is / My / Account / For / Java/";
       // String[] arr = str.split("|");

        for(String item : str1.split("|")) {
            System.out.print(item);
        }
    }
}

The program works correctly with the string str2, but does not work with the string str1. What are the possible threads in this program?

+4
source share
3 answers

String#split() . | . , , |, \| . \ Java. , Java , \| , "\\|" String#split().

+23

str1.split("\\|")

str1.split("|")

|

+12

split , | , . str.split("\\|"). :

>>> Arrays.asList("This | is | My | Account | For | Java|".split("\\|"));
[This ,  is ,  My ,  Account ,  For ,  Java]
+7

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


All Articles