Java: Remove full stop in string

后端 未结 5 1470
死守一世寂寞
死守一世寂寞 2021-01-18 23:34

I want to delete all the full stops ( . ) in a string.

Therefore I tried: inpt = inpt.replaceAll(\".\", \"\");, but instead of deleting only the full st

5条回答
  •  春和景丽
    2021-01-19 00:33

    Don't use replaceAll(), use replace():

    inpt = inpt.replace(".", "");
    

    It is a common misconception that replace() doesn't replace all occurrences, because there's a replaceAll() method, but in fact both replace all occurrences. The difference between the two methods is that replaceAll() matches on a regex (fyi a dot in regex means "any character", which explains what you were experiencing) whereas replace() matches on a literal String.

提交回复
热议问题