replace all + with -

后端 未结 6 1470
滥情空心
滥情空心 2021-01-29 06:24

I am trying to replace a + character into a hyphen I have in my string.

String str = \"word+word\";
str.replaceAll(\'+ \', \'-\');
         


        
6条回答
  •  暖寄归人
    2021-01-29 07:09

    The replaceAll function takes a regular expression as its first argument. It so happens that + is a special character in regular expression language. Try replacing + with \\+. This will escape the plus sign, thus making the code to treat it like a normal character.

    Also, the replaceAll method yields a string, so that will not work. Try doing:

    String str = "word+word";
    str = str.replaceAll("\\+ ", "-");
    

提交回复
热议问题