移除& # 39; & # 39;c#中字符串的字符

本文关键字:字符 字符串 移除 | 更新日期: 2023-09-27 18:16:44

我有以下代码

string line = ""; 
while ((line = stringReader.ReadLine()) != null)
{
    // split the lines
    for (int c = 0; c < line.Length; c++)
    {
        if ( line[c] == ',' && line[c - 1] == '"' && line[c + 1] == '"')
        {
            line.Trim(new char[] {''''}); // <------
            lineBreakOne = line.Substring(1, c  - 2);
            lineBreakTwo = line.Substring(c + 2, line.Length - 2);
        }
    }
}

我在我想知道的那行添加了一个评论网。我想从字符串中删除所有'''字符。这是正确的方法吗?我不工作。

移除& # 39; & # 39;c#中字符串的字符

您可以使用:

line.Replace(@"'", "");

line.Replace(@"'", string.Empty);

您可以使用String。Replace基本上删除所有出现的

line.Replace(@"'", ""); 

要从字符串中删除所有''',只需执行以下操作:

myString = myString.Replace("''", "");
line = line.Replace("''", "");

为什么不这么简单呢?

resultString = Regex.Replace(subjectString, @"''", "");

尝试替换

string result = line.Replace("''","");

尝试使用

String sOld = ...;
String sNew =     sOld.Replace("''", String.Empty);

我已经面对这个问题很多次了,我很惊讶,其中很多都不起作用。

我只是用Newtonsoft对字符串进行反序列化。Json和我得到明文。

string rough = "'"call 12'"";
rough = JsonConvert.DeserializeObject<string>(rough);
//the result is: "call 12";

Trim只删除字符串开头和结尾的字符,这就是为什么你的代码不能完全工作。您应该使用Replace:

line.Replace(@"'", string.Empty);
         while ((line = stringReader.ReadLine()) != null)
         {
             // split the lines
             for (int c = 0; c < line.Length; c++)
             {
                 line = line.Replace("''", "");
                 lineBreakOne = line.Substring(1, c - 2);
                 lineBreakTwo = line.Substring(c + 2, line.Length - 2);
             }
         }