简体   繁体   中英

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

 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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