How to replace a plus character using Java's String.replaceAll method

后端 未结 8 1895
别跟我提以往
别跟我提以往 2020-12-28 11:50

What\'s the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java\'s replaceAll method in the String class? I can\'t

8条回答
  •  爱一瞬间的悲伤
    2020-12-28 12:37

    You need to escape the + for the regular expression, using \.

    However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:

    "\\+"
    

提交回复
热议问题