C# Ignore Case and Check if String contains Substring
To ignore case and check if specified substring value
is present in the string str
, use String.Contains(String, StringComparison) method.
Prepare StringComparison
object comp
with StringComparison.OrdinalIgnoreCase
. Call Contains
method on the string str
and pass the substring value
, and string comparison comp
objects as arguments.
Example
If the substring is present inside the string, of course ignoring the case, then String.Contains()
method returns True
, else, it returns False
.
C# Program
using System; class Example { static void Main(string[] args) { String str = "abcdefgh"; String value = "CD"; StringComparison comp = StringComparison.OrdinalIgnoreCase; Boolean result = str.Contains(value, comp); Console.WriteLine($"Does string contain specified substring? {result}"); } }
Output
Does string contain specified substring? True
ADVERTISEMENT
Conclusion
In this C# Tutorial, we learned how to check if specified substring is present in a given string while ignoring the case during string comparison.