简体   繁体   English

如何将以下字符替换为空白并在下一行中写入?

[英]How to replace the following character with an empty space and write in next line?

How to replace that character which is square in shape (□) with a \\n or an empty space and write in next line using c#?...It is writing in text file with □... Here is my code.... 如何用\\ n或空格替换方形(□)字符并使用c#在下一行写?...正在用□...写入文本文件中这是我的代码... 。

 string line = dt.Rows[0].ItemArray[0].ToString().Replace(Environment.NewLine, "<br>");

and

 if (line.IndexOf(line2, StringComparison.CurrentCultureIgnoreCase) != -1)
    {
        if (!patternwritten)
        {
            dest.WriteLine("");
            dest.WriteLine("Pattern Name : " + line2 );
             patternwritten = true;
         }

  dest.WriteLine("LineNo : " + counter.ToString() + " : " + "" + line.TrimStart());
}

But it is writing the whole line with □....Any suggestion?? 但这是用□.....任何建议写整行的内容??

My guess is that your string contains simple linefeed characters, whereas your Environment.NewLine value is "\\r\\n". 我的猜测是您的字符串包含简单的换行字符,而您的Environment.NewLine值为“ \\ r \\ n”。

One simple way of coping with this is to perform two replacements: 解决此问题的一种简单方法是执行两次替换:

text = text.Replace("\r\n", "<br>")
           .Replace("\n", "<br>")

To find our for sure what's going on, you should open up your file in a binary file editor and see what binary data is in the file. 要找到我们肯定发生了什么,你应该在一个二进制文件编辑器打开你的文件,看到的是该文件在什么二进制数据。 My guess is that you'll find a character represented by 0x0A in binary. 我的猜测是,您会找到一个由0x0A表示的二进制字符。

It seems to me that you are having problems with Unix newlines. 在我看来,您在使用Unix换行符时遇到问题。

Try this using this method: 使用以下方法尝试以下方法:

public static string ConvertToWindowsNewlines(string value)
{
    value = value.Replace("\r\n", "\n");
    value = value.Replace("\n", "\r\n");
    return value;
}

EDIT: Found solution in: http://dotnetperls.com/whitespace . 编辑:在以下位置找到了解决方案: http : //dotnetperls.com/whitespace

Contains some more tips on C# and char replacement. 包含有关C#和char替换的更多提示。

使用Trim()而不是TrimStart() ,它将修剪掉结尾

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

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