Replace Space to Hyphen

前端 未结 7 1118

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

String replaceText = \"AT AT\";
replaceText.replace(\' \', \'-\');

7条回答
  •  执笔经年
    2021-02-18 23:41

    The replace and replaceAll methods return a String with the replaced result. Are you using the returned value, or expecting the replaceText String to change? If it's the latter, you won't see the change, because Strings are immutable.

    String replaceText = "AT AT";
    String replaced = replaceText.replace(' ', '-');
    
    // replaced will be "AT-AT", but replaceText will NOT change
    

提交回复
热议问题