Search for a newline Character C#.net

"yo\n" // output as "yo" + newline
"yo\n".IndexOf('\n') // returns 2
"yo\\n" // output as "yo\n"
"yo\\n".IndexOf('\n') // returns -1

Are you sure you're searching yo\n and not yo\\n?

Edit

Based on your update, I can see that I guessed correctly. If your string says:

printf("yo\n");

... then this does not contain a newline character. If it did, it would look like this:

printf("yo
");

What it actually has is an escaped newline character, or in other words, a backslash character followed by an 'n'. That's why the string you're seeing when you debug is "\tprintf(\"yo\\n\");". If you want to find this character combination, you can use:

line.IndexOf("\\n")

For example:

"\tprintf(\"yo\\n\");" // output as "  printf("yo\n");"
"\tprintf(\"yo\\n\");".IndexOf("\\n") // returns 11

Looks like your line does not contain a newline.

If you are using File.ReadAllLines or string.Split on newline, then each line in the returned array will not contain the newline. If you are using StreamReader or one of the classes inheriting from it, the ReadLine method will return the string without the newline.

string lotsOfLines = @"one
two
three";

string[] lines = lotsOfLines.Split('\n');

foreach(string line in lines)
{
  Console.WriteLine(line.IndexOf('\n'); // prints -1 three times
}

That should work although in Windows you'll have to search for '\r\n'.

-1 simply means that no enter was found.