replace all + with -

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

    Use

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

    A few errors in your code :

    • replaceAll takes strings, not chars
    • the + char must be escaped as the first argument is a regular expression (and \ itself must be escaped in java string literals)
    • you must take the return of the function : as String is immutable the function doesn't change it but returns another string

提交回复
热议问题