Split string with | separator in java

前端 未结 12 760
旧时难觅i
旧时难觅i 2020-12-06 04:11

I have a string that\'s like this: 1|\"value\"|;

I want to split that string and have chosen | as the separator.

My code looks like

12条回答
  •  青春惊慌失措
    2020-12-06 04:44

    It won't work this way, because you have to escape the Pipe | first. The following sample code, found at (http://www.rgagnon.com/javadetails/java-0438.html) shows an example.

    public class StringSplit {
      public static void main(String args[]) throws Exception{
        String testString = "Real|How|To";
        // bad
        System.out.println(java.util.Arrays.toString(
            testString.split("|")
        ));
        // output : [, R, e, a, l, |, H, o, w, |, T, o]
    
        // good
        System.out.println(java.util.Arrays.toString(
          testString.split("\\|")
        ));
        // output : [Real, How, To]
      }
    }
    

提交回复
热议问题