【问题标题】:Java "||" String replace by "OR" [duplicate]爪哇“||”字符串替换为“OR”[重复]
【发布时间】:2016-09-30 08:06:24
【问题描述】:

我有一个类似

的字符串

"年龄 = 18 || 姓名 = 'Mistic' || 公民身份 = '已婚' || 性别 = '0' "

我需要替换“||”通过“或”。我尝试了以下代码。

System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0'".replaceAll("||", "OR"));

但我明白了

"ORaORgOReOR OR=OR OR1OR8OR OR|OR|OR ORnORaORmOREOR OR=OR OR'ORMORiORsORtoriORcOR'OR OR|OR|OR ORCORIORVORIORLORsORTORAORTORUORsOR OR=OR OR'ORmORaORrORrORiOReORdOR'OR OR|OR|OR ORgOReORnORdOReORrOR OR=OR OR'OR0OR'OR"

我需要的是

“年龄 = 18 或姓名 = 'Mistic' 或公民身份 = '已婚' 或性别 = '0' "

我怎样才能做到这一点。

编辑 我已经阅读了this question 的问题和答案,它并不相似。因为那个问题是关于替换字符串,而我的问题是关于让我的代码得到不熟悉的结果。

【问题讨论】:

  • @ErwinBolwidt 这个问题与我的问题不重复。
  • 为什么不是重复的?该问题的OP与您有同样的困惑,答案是相同的。您还应该查看您自己问题的最高投票答案。重复的答案更好地解释了为什么您可能会在 replaceAllreplace 之间感到困惑。
  • 这是因为replaceAll 采用正则表达式,例如split。查看链接的问题。

标签: java string replaceall


【解决方案1】:

当您不想用正则表达式替换时,为什么要使用replaceAll

尝试使用普通的replace

System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' 
    || gender = '0'".replace("||", "OR"));

输出

年龄 = 18 或姓名 = 'Mistic' 或公民身份 = '已婚' 或性别 = '0'

【讨论】:

    【解决方案2】:

    | 在正则表达式中具有特殊含义。你需要逃避它。

    public static void main(String[] args) {
        String s = "age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' ";
        System.out.println(s.replaceAll("\\|\\|", "OR"));
    }
    

    O/P:

    age = 18 OR name = 'Mistic' OR civilstatus = 'married' OR gender = '0' 
    

    PS : 或者,您可以使用Pattern.quote() 转义特殊字符。

    String  st = Pattern.quote("||");
    System.out.println(s.replaceAll(st, "OR"));
    

    【讨论】:

      【解决方案3】:

      您必须转义“||”,因为它们是正则表达式中的元字符。

      用途:

      System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0'".replaceAll("\\|\\|", "OR"));
      

      你得到的原因:

      "ORaORgOReOR OR=OR OR1OR8OR OR|OR|OR ORnORaORmOREOR OR=OR OR'ORMORiORsORtoriORcOR'OR OR|OR|OR ORCORIORVORIORLORsORTORAORTORUORsOR OR=OR OR'ORmORaORrORrORiOReORdOR'OR OR|OR|OR ORgOReORnORdOReORrOR OR=OR OR'OR0OR'OR"

      是因为正则表达式“||”用“OR”表示“替换空字符串或空字符串或空字符串”。换句话说,用“OR”替换所有空字符串。

      【讨论】:

        【解决方案4】:

        你可以用replace代替replaceAll,试试下面的..

            System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' ".replace("||", "OR"));
        

        【讨论】:

        • 你为什么要分配一个字符串,然后甚至不使用它?
        • 哦,是的,谢谢你不需要带字符串,我更正了..
        【解决方案5】:

        使用Pattern.quote

        System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0'".replaceAll(Pattern.quote("||"), "OR"));
        

        【讨论】:

          【解决方案6】:

          您需要对输入字符串中的特殊字符进行转义。

          【讨论】:

            【解决方案7】:

            您可以使用以下代码替换所有'||'用“或”

            String str = "age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' ";
            System.out.println(str.replaceAll("\\|\\|", "OR"));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-10-12
              • 2021-08-19
              • 2013-08-25
              • 2014-02-13
              • 2022-01-10
              • 2011-06-24
              • 1970-01-01
              相关资源
              最近更新 更多