tags:

views:

167

answers:

4

I am somehow unable to determine whether a string is newline or not. The string which I use is read from a file written by Ultraedit using DOS Terminators CR/LF. I assume this would equate to "\r\n" or Environment.NewLine in C#. However , when I perform a comparison like this it always seem to return false :

if(str==Environment.NewLine)

Anyone with a clue on what's going on here?

+1  A: 

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

Evgeny
+1  A: 

Newline is "\r\n", not "/r/n". Maybe there's more than just the newline.... what is the string value in Debug Mode?

You could use the new .NET 4.0 Method: String.IsNullOrWhiteSpace

Steav
+5  A: 

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

Daniel Renshaw
I'm using File.ReadAllLines to read them into a string array
paradox
Daniel got the correct answer. File.ReadAllLine won't include the newline into the string. So you should actually check for String.Empty.
Amry
when you use File.ReadAllLines, why do you care about '\n'. You will not get that. Instead you'll get all the lines in a string array split by '\n'. check if File.ReadAllText serves your purpose.
Veer
A: 

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

Oliver