简体   繁体   English

如何在文件中替换2个双引号

[英]how to replace 2 double quotes in a file

I have 2 double quotes that need to be replaced by a single double quote. 我有2个双引号需要替换为一个双引号。

I am using this method: 我正在使用这种方法:

private static void ReplaceTextInFile(string originalFile, string outputFile, string searchTerm, string replaceTerm)
{
    string tempLineValue;
    using (FileStream inputStream = File.OpenRead(originalFile))
    {
        using (StreamReader inputReader = new StreamReader(inputStream))
        {
            using (StreamWriter outputWriter = File.AppendText(outputFile))
            {
                while (null != (tempLineValue = inputReader.ReadLine()))
                {
                    outputWriter.WriteLine(tempLineValue.Replace(searchTerm, replaceTerm));
                }
            }
        }
    }
}

and calling it this way 并这样称呼它

ReplaceTextInFile(file, file + "new", (char)34 + (char)34, (char)34);

the error i am getting is 我得到的错误是

Error   4   Argument '3': cannot convert from 'int' to 'string' 

and  Error  5   Argument '4': cannot convert from 'char' to 'string'    

what am i doing wrong? 我究竟做错了什么?

(char)34+(char)34 is an int , with value 68. (char)34+(char)34是一个int ,值为68。
The function is expecting a string. 该函数需要一个字符串。

I would pass "\\"\\"" 我会通过"\\"\\""
(escaping each double-quote with a back-slash) (用反斜杠将每个双引号引起来)

ReplaceTextInFile(file, file + "new", "\"\"", "\"");

我会使用ReplaceTextInFile(file, file + "new", "\\"\\"", "\\"");

You're calling this method with an Int32 ( char + char == int ) and a Char , not two string values. 您正在使用Int32char + char == int )和Char而不是两个string值来调用此方法。 Why not just use?: 为什么不只是使用?:

ReplaceTextInFile(file, file + "new", "\"\"", "\"");

The function expects four string s. 该函数需要四个string s。 You pass two string s, and then an int , and then a char . 您传递两个string s,然后传递一个int ,然后传递一个char

Adding two char s results in an int . 将两个char得出一个int You can't concatenate chars, it makes no sense as a char represents an individual character, so the result is an int . 您不能连接char,因为char代表单个字符,所以没有意义,因此结果是int

The last one is a straight cast to char where a string is expected, that seems obvious enough. 最后一个是直接转换为char ,其中应该有string ,这似乎很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM