要从删除字符串的双引号 [英] Want to remove the double quotes from the strings

查看:148
本文介绍了要从删除字符串的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些问题,这里涉及到去除双引号。但它没有解决我的问题。他们告诉使用更换或修剪功能。我使用,但问题仍然存在。

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.

我的代码:

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();

我尝试用 rowString.Replace('','').Trim(); 在上面的代码。但是,双引号都存在,因为它是。 Proveide我一个解决方案。谢谢

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

推荐答案

您需要分配回 rowString

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

字符串不变

Strings are immutable.

row.String.Replace(...)将返回一个字符串,因为你没有指定任何东西就会被丢弃,不会改变原有的 rowString 对象。

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 rowString object.

您可以使用的String.Empty 或<$ 。C $ C>,为空字符串替换,而不是单个空格双引号,所以你的说法应该是:

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

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

(记住要传递的双引号作为一个字符串 \,由于与该的String.Empty方法重载既需要的参数是字符串类型)。

(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.replace 在字符串的开头或结尾。

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

这篇关于要从删除字符串的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆