如何将单引号转换为破折号并删除双引号?

时间:2022-09-15 16:10:04

I want to convert single quote to dash and remove double quotes embedded in the string.

我想将单引号转换为破折号并删除字符串中嵌入的双引号。

For example, if i have a string 8'5" it should be "8-5"

例如,如果我有一个字符串8'5“它应该是”8-5“

4 个解决方案

#1


5  

Please use:

请用:

str = str.Replace('\'', '-').Replace("\"", string.Empty)

#2


1  

You need the \ to escape the ', then you can use string.Replace:

你需要\来逃避',然后你可以使用string.Replace:

string original = "8'5";
string newString = original.Replace('\'', '-');

Demo

演示

#3


1  

Have you tried looking at any C# language reference? What you are asking is very basic. The following code is a quick and dirty way of doing what you are asking:

您是否尝试过查看任何C#语言参考?你问的是非常基本的。以下代码是一种快速而肮脏的方式来执行您所要求的操作:

string measurement = "8'5\"";
measurement = measurement.Replace("'", "-").Replace("\"", "");

The back slash is used to escape the double quote.

反斜杠用于逃避双引号。

#4


0  

For better understanding:

为了更好地理解:

        string str = "8\'5\"";

        //one row version
        str = str.Replace('\'', '-').Insert(0, "\"");
        Console.WriteLine(str);            

        //multi row version, just for understanding
        str = "8\'5\"";            
        str = str.Replace('\'', '-');            
        str = str.Insert(0, "\"");

#1


5  

Please use:

请用:

str = str.Replace('\'', '-').Replace("\"", string.Empty)

#2


1  

You need the \ to escape the ', then you can use string.Replace:

你需要\来逃避',然后你可以使用string.Replace:

string original = "8'5";
string newString = original.Replace('\'', '-');

Demo

演示

#3


1  

Have you tried looking at any C# language reference? What you are asking is very basic. The following code is a quick and dirty way of doing what you are asking:

您是否尝试过查看任何C#语言参考?你问的是非常基本的。以下代码是一种快速而肮脏的方式来执行您所要求的操作:

string measurement = "8'5\"";
measurement = measurement.Replace("'", "-").Replace("\"", "");

The back slash is used to escape the double quote.

反斜杠用于逃避双引号。

#4


0  

For better understanding:

为了更好地理解:

        string str = "8\'5\"";

        //one row version
        str = str.Replace('\'', '-').Insert(0, "\"");
        Console.WriteLine(str);            

        //multi row version, just for understanding
        str = "8\'5\"";            
        str = str.Replace('\'', '-');            
        str = str.Insert(0, "\"");