替换java中字符串中所有出现的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19599341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:39:35  来源:igfitidea点击:

replace all occurrences of a character in a string in java?

java

提问by frazman

A very noob question..

一个非常菜鸟的问题..

I want to replace all the occurance of "." in a string with empty space..

我想替换所有出现的“。” 在一个有空格的字符串中..

So this is what I tried

所以这就是我尝试过的

             String s = "1.2.3.4";
        System.out.println(s);
         s = s.replaceAll(".", " ");
        System.out.println(s);

But the second print is an empty print?

但是第二个打印是空打印?

What did i missed here?

我在这里错过了什么?

采纳答案by Terry Li

You want to escape the .. Otherwise, it can match anything.

你想逃避.. 否则,它可以匹配任何东西。

Try s.replaceAll("\\.", " ")instead.

试试吧s.replaceAll("\\.", " ")

回答by Evgeniy Dorofeev

Use String.replace(char, char) instead of String.replaceAll

使用 String.replace(char, char) 而不是 String.replaceAll