-5

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

For example:

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

Boolean bool = str1.Contains(str2);




4
  • 5
    in the time it took to write this question - couldn't you just have fixed the obvious error and tried it? or looked up The Manual, saying This method performs an ordinal (case-sensitive and culture-insensitive) comparison.? Commented Apr 27, 2022 at 11:52
  • According to the docs for that method - "This method performs an ordinal (case-sensitive and culture-insensitive) comparison." Commented Apr 27, 2022 at 11:54
  • Try out. tutorialspoint.com/compile_csharp_online.php Commented Apr 27, 2022 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. Commented Apr 27, 2022 at 12:16

2 Answers 2

0

From Microsoft's official documentation:

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

read it for more..

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.