2

SDK: .net core 3.1 Environment: ubuntu 18/04

code:

Console.WriteLine("abc\n".IndexOf("\n"));
Console.WriteLine("abc\r\n".IndexOf("\n"));
Console.WriteLine("abc\r\n".Contains("\n"));
Console.WriteLine("abc\r\n".Split('\n').Length);
Console.WriteLine("abc\n".EndsWith("\n"));
Console.WriteLine("abc\r\n".EndsWith("\n"));

result:

3
-1   -> not as expected
True
2
True
False   -> not as expected
  • This behavior can be related to different handling of EOL symbols, have a look at this [thread](https://stackoverflow.com/questions/1761051/difference-between-n-and-r) – Pavel Anikhouski Apr 14 '20 at 09:05

1 Answers1

0

You need to use StringComparison.OrdinalIgnoreCase. Read this and you will understand what is happening.

Console.WriteLine("abc\n".IndexOf("\n", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("abc\r\n".IndexOf("\n", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("abc\r\n".Contains("\n", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("abc\r\n".Split('\n').Length);
Console.WriteLine("abc\n".EndsWith("\n", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("abc\r\n".EndsWith("\n", StringComparison.OrdinalIgnoreCase));

https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings

cdev
  • 5,043
  • 2
  • 33
  • 32