Determine if a string is a newline character in C #

I somehow cannot determine if a string is a newline or not. The line I'm using is read from a file written by Ultraedit using DOS Terminators CR / LF. I assume this will be equal to "\ r \ n" or Environment.NewLine in C #. However, when I perform such a comparison, it always returns false:

if(str==Environment.NewLine) 

Anyone who knows what is going on here?

+4
source share
4 answers

How are lines read? If you use StreamReader.ReadLine (or something similar), the new string character will not appear in the resulting string - it will be String.Empty or (i.e. "").

+7
source

Are you sure that the whole line contains only NewLine and nothing more or less? Have you tried str.Contains(Environment.NewLine) ?

+4
source

The new line is "\ r \ n", not "/ r / n". Maybe there is more than just a new line .... what is a string value in debug mode?

You can use the new .NET 4.0 method: String.IsNullOrWhiteSpace

+2
source

The most obvious troubleshooting step would be to check what the value of str . Just view it in the debugger or print it.

+1
source

Source: https://habr.com/ru/post/1308442/


All Articles