开发者

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 ne开发者_运维知识库xt line using c#?...It is writing in text file with □... Here is my code....

 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".

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.


It seems to me that you are having problems with Unix newlines.

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.

Contains some more tips on C# and char replacement.


Use Trim() rather than TrimStart(), and it will trim it off the end

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜