美文网首页
String: split 用 "|"

String: split 用 "|"

作者: 鱼da王 | 来源:发表于2019-02-19 10:08 被阅读0次
  1. String类,split方法,参数"|",没有达到预想的额结果

  2. 情况如图:


    string分隔符分隔.png
  3. 原因是:split方法参数是regex,因此regex符号需要转义,使用“\\|”可得到正确的结果。

    注意:除了使用“|”需要转义,例如"\"也需要转义,普通字符串"\\", regex表达式"\\\",另外还有"+"、"*",也需要转义。

  4. 代码示例:

    public static void main(String[] args) {
    
            String str = "ABC\\DEF";
            String[] value = str.split("\\\\");
            for(String s : value){
                System.out.println(s);
            }
    }
    

相关文章

网友评论

      本文标题:String: split 用 "|"

      本文链接:https://www.haomeiwen.com/subject/tofxyqtx.html