-5

Is the string-method .Contains case-sensitive in .Net?

For example:

string str1 = "tEst";
string str2 = "Te"; 

Boolean bool = str1.Contains(str2);




stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 5
    in the time it took to write this question - couldn't you just have fixed the obvious error and [tried it](https://dotnetfiddle.net/7XJbYB)? or looked up [The Manual](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-6.0), saying `This method performs an ordinal (case-sensitive and culture-insensitive) comparison.`? – Franz Gleichmann Apr 27 '22 at 11:52
  • According to the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-6.0#system-string-contains(system-string)) for that method - _"This method performs an ordinal (case-sensitive and culture-insensitive) comparison."_ – stuartd Apr 27 '22 at 11:54
  • Try out. https://www.tutorialspoint.com/compile_csharp_online.php – Jonas Metzler Apr 27 '22 at 11:54
  • 1
    FYI, if you use it in a EF Linq query then it will be translated to SQL and the case sensitivity will be determined by the DB which usually defaults to case insensitive. – juharr Apr 27 '22 at 12:16

2 Answers2

0

From Microsoft's official documentation:

Remarks This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

read it for more..

Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17
0

Yes. By default, string comparisons are always case sensitive in the framework. The recommended way is to get case insensitivity is to use a StringComparison or a stringComparer to specify case insensitivity.

Versions of .Net older than .Net core 2.1 lack a .Contains overload that takes such an argument. A workaround is to use .IndexOf and check that it returns a index >= 0, since that can take a StringComparison argument.

JonasH
  • 28,608
  • 2
  • 10
  • 23