replace all + with -

后端 未结 6 1465
滥情空心
滥情空心 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:21

    Just use replace:

    str = str.replace('+', '-');
    

    This one doesn't work on regex but take characters as they are.
    Also as you see you have to reassing value again to your str variable because String in Java are immutable. In this case method replace doesn't change current String (str) but create new one with replaced + to '-'.

提交回复
热议问题