C# 想要从字符串中删除双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12740486/
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-10 00:17:32  来源:igfitidea点击:

Want to remove the double quotes from the strings

c#stringc#-4.0replacec#-3.0

提问by

I saw some questions here related to removing double quotes. But it not solved my Issue. They told to use Replace or Trim functions. I used but the problem remains.

我在这里看到了一些与删除双引号相关的问题。但它没有解决我的问题。他们告诉使用替换或修剪功能。我用过,但问题仍然存在。

My code:

我的代码:

DataTable dt = TypeConveriontHelper.ConvretExcelToDataTable(SourceAppFilePath);
        string targetPath = BatchFolderPath + @"\" + "TRANSACT.INX";
        StreamWriter wrtr = null;
        wrtr = new StreamWriter(targetPath);
        for (int x = 0; x < dt.Rows.Count; x++)
        {
            string rowString = "";
            for (int y = 0; y < dt.Columns.Count; y++)
            {
                if (y == dt.Columns.Count - 1)
                {
                    rowString += "\"" + dt.Rows[x][y].ToString() + "\"";
                }
                else
                {
                    rowString += "\"" + dt.Rows[x][y].ToString() + "\"~";
                }
            }           
            rowString.Replace('"', ' ').Trim();
            wrtr.WriteLine(rowString);
        }
        wrtr.Close();
        wrtr.Dispose();

I tried by using rowString.Replace('"', ' ').Trim();in above code. But double quotes are present as it is. Proveide me a solution. Thanks.

我尝试rowString.Replace('"', ' ').Trim();在上面的代码中使用。但是双引号按原样存在。给我一个解决方案。谢谢。

采纳答案by Habib

You need to assign it back to rowString:

您需要将其分配回rowString

rowString = rowString.Replace('"', ' ').Trim();

Stringsare immutable.

字符串不可变的

row.String.Replace(...)will return you a string, since you are not assigning it anything it will get discarded. It will not change the original rowStringobject.

row.String.Replace(...)将返回一个字符串,因为您没有为其分配任何内容,它将被丢弃。它不会改变原始rowString对象。

You may use String.Emptyor ""to replace double quotes with an empty string, instead of single space ' '. So your statement should be:

您可以使用String.Empty""将双引号替换为空字符串,而不是单个空格' '。所以你的陈述应该是:

rowString = rowString.Replace("\"", string.Empty).Trim();

(Remember to pass double quote as a string"\"", since the method overload with string.Empty will require both the parameters to be of type string).

(记住将双引号作为字符串传递"\"",因为使用 string.Empty 的方法重载将要求两个参数都是字符串类型)。

You may get rid of Trim()at the end, if you were trying to remove spaces adding during string.Replaceat the beginning or end of the string.

Trim()如果您试图删除string.Replace在字符串开头或结尾添加的空格,则可能会在末尾删除。

回答by Jonas W

This row contains a error.

此行包含错误。

rowString.Replace('"', ' ').Trim(); 

Since both replace and trim returns a string you need to assign this to a variable. By not assigning the string you will loose the value generated by the replace and trim methods.

由于 replace 和 trim 都返回一个字符串,因此您需要将其分配给一个变量。通过不分配字符串,您将丢失替换和修剪方法生成的值。

var myString = rowString.Replace('"', ' ').Trim(); 
//Or
rowString = rowString.Replace('"', ' ').Trim(); 

回答by ALI VOJDANIANARDAKANI

In VB you must try this :

在VB中你必须试试这个:

Replace("""", "");

But in C# you must try this :

但是在 C# 中你必须试试这个:

Replace("\"", "");

回答by Prabhu Murthy

rowString.Replace('"', ' ')creates a new string object and do not modify the existing string object.so you will have to assign the newly created string object to a different variable or overwrite your existing variable.

rowString.Replace('"', ' ')创建一个新的字符串对象并且不要修改现有的字符串对象。因此您必须将新创建的字符串对象分配给不同的变量或覆盖现有的变量。

rowString=rowString.Replace('"', ' ').Trim()    
or
var somestring=rowString.Replace('"', ' ').Trim()

回答by Dhanasekar

As Habib mentioned, Strings are immutable. Any string manipulation needs to be set back with the manipulated data like Replace,Reverse, etc....

正如 Habib 所提到的,字符串是不可变的。任何字符串操作都需要使用被操作的数据(如替换、反转等)进行设置。...

So try assigning the replaced string to get your desired behavior acheived.

因此,尝试分配替换的字符串以实现您想要的行为。

回答by pants

Trim can remove any character(s), not just whitespace.

Trim 可以删除任何字符,而不仅仅是空格。

myString = myString.Trim('"');

http://msdn.microsoft.com/en-us/library/d4tt83f9%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/d4tt83f9%28v=vs.110%29.aspx

回答by Chris H

Your issue is a common mistake. Here's what you need to do:

你的问题是一个常见的错误。您需要执行以下操作:

rowString = rowString.Replace('"', ' ').Trim();

Be sure to return your result to a variable(rowString =). If you do not do this, C# will still run the methods but it will not modify the variable. This is the case with any immutable objects.

请务必将结果返回到变量( rowString =)。如果不这样做,C# 仍将运行这些方法,但不会修改变量。任何不可变对象都是这种情况。

Example:

例子:

rowString.Replace('"', ' ').Trim();

will notmodify the actual rowStringobject.

不会修改实际的rowString对象。

rowString = rowString.Replace('"', ' ').Trim();

replaces the old value of rowStringwith the result of the code on the right.

rowString右边代码的结果替换旧的值。

I wrote this answer and just realized that Habib explained the exact same thing above. :)

我写了这个答案,刚刚意识到 Habib 解释了上面完全相同的事情。:)

回答by Jpsy

You can use Trim()to simultaneously remove any combination of whitespaces, single quotes and double quotes:

您可以使用Trim()来同时删除空格、单引号和双引号的任意组合:

rowString.Trim('"', '\'', ' ')