Java: Remove full stop in string

后端 未结 5 1432
死守一世寂寞
死守一世寂寞 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:09

    replaceAll takes a regular expressions as an argument, and . in a regex means "any character".

    You can use replace instead:

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

    It will remove all occurences of ..

    0 讨论(0)
  • 2021-01-19 00:10

    replaceAll use a regex, please use the following:

    inpt = inpt.replaceAll("\\.", "")
    
    0 讨论(0)
  • 2021-01-19 00:13

    I think you have to mask the dot

    inpt = inpt.replaceAll("\\.", "");
    
    0 讨论(0)
  • 2021-01-19 00:21

    String#replaceAll(String, String) takes a regex. The dot is a regex meta character that will match anything.

    Use

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

    it will also replace every dot in your inpt, but treats the first parameter as a literal sequence, see JavaDoc.

    If you want to stick to regex, you have to escape the dot:

    inpt = inpt.replaceAll("\\.", "");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题