如何用Java中的空格替换双引号?

时间:2022-09-15 15:43:42

For example:

例如:

"I don't like these "double" quotes"

and I want the output to be

我希望输出是

I don't like these double quotes

4 个解决方案

#1


27  

Use String#replace().

使用字符串#替换()。

To replace them with spaces (as per your question title):

用空格替换它们(根据您的问题标题):

System.out.println("I don't like these \"double\" quotes".replace("\"", " "));

The above can also be done with characters:

以上也可以用字符完成:

System.out.println("I don't like these \"double\" quotes".replace('"', ' '));

To remove them (as per your example):

删除它们(如您的示例所示):

System.out.println("I don't like these \"double\" quotes".replace("\"", ""));

#2


9  

You don't need regex for this. Just a character-by-character replace is sufficient. You can use String#replace() for this.

你不需要regex。只需逐个字符替换即可。您可以为此使用字符串#replace()。

String replaced = original.replace("\"", " ");

Note that you can also use an empty string "" instead to replace with. Else the spaces would double up.

注意,您还可以使用一个空字符串“”替换为。否则空间就会加倍。

String replaced = original.replace("\"", "");

#3


2  

You can do it like this:

你可以这样做:

string tmp = "Hello 'World'";
tmp.replace("'", "");

But that will just replace single quotes. To replace double quotes, you must first escape them, like so:

但这将取代单引号。要替换双引号,您必须首先转义它们,如下所示:

string tmp = "Hello, \"World\"";
tmp.replace("\"", "");

You can replace it with a space, or just leave it empty (I believe you wanted it to be left blank, but your question title implies otherwise.

你可以用空格替换它,或者只是让它保持为空(我相信你想让它保持为空,但是你的问句名暗示了相反的情况。

#4


2  

Strings are immutable, so you need to say

字符串是不可变的,所以你需要说

sInputString = sInputString("\"","");

not just the right side of the =

不只是=的右边

#1


27  

Use String#replace().

使用字符串#替换()。

To replace them with spaces (as per your question title):

用空格替换它们(根据您的问题标题):

System.out.println("I don't like these \"double\" quotes".replace("\"", " "));

The above can also be done with characters:

以上也可以用字符完成:

System.out.println("I don't like these \"double\" quotes".replace('"', ' '));

To remove them (as per your example):

删除它们(如您的示例所示):

System.out.println("I don't like these \"double\" quotes".replace("\"", ""));

#2


9  

You don't need regex for this. Just a character-by-character replace is sufficient. You can use String#replace() for this.

你不需要regex。只需逐个字符替换即可。您可以为此使用字符串#replace()。

String replaced = original.replace("\"", " ");

Note that you can also use an empty string "" instead to replace with. Else the spaces would double up.

注意,您还可以使用一个空字符串“”替换为。否则空间就会加倍。

String replaced = original.replace("\"", "");

#3


2  

You can do it like this:

你可以这样做:

string tmp = "Hello 'World'";
tmp.replace("'", "");

But that will just replace single quotes. To replace double quotes, you must first escape them, like so:

但这将取代单引号。要替换双引号,您必须首先转义它们,如下所示:

string tmp = "Hello, \"World\"";
tmp.replace("\"", "");

You can replace it with a space, or just leave it empty (I believe you wanted it to be left blank, but your question title implies otherwise.

你可以用空格替换它,或者只是让它保持为空(我相信你想让它保持为空,但是你的问句名暗示了相反的情况。

#4


2  

Strings are immutable, so you need to say

字符串是不可变的,所以你需要说

sInputString = sInputString("\"","");

not just the right side of the =

不只是=的右边