Why does .split(“\\”) generate an exception?

前端 未结 5 1488
死守一世寂寞
死守一世寂寞 2021-01-25 03:35

I have a String representing a directory, where \\ is used to separate folders. I want to split based on \"\\\\\":

String address = \"C         


        
5条回答
  •  既然无缘
    2021-01-25 04:10

    String#split() method takes a regex. In regex, you need to escape the backslashes. And then for string literals in Java, you need to escape the backslash. In all, you need to use 4 backslashes:

    String[] splited = address.split("\\\\");
    

提交回复
热议问题