How to use StringComparison for strings in C #?

string body = Selenium.GetBodyText(); if (body.Contains("software", StringComparison.CurrentCultureIgnoreCase)) { //do something } 

I get the message string does not contain a definition for Contains when I do this. What am I doing wrong here? Thanks in advance to everyone!

I am trying to check if the body has the string “software”, “software” or “SOFTWARE”. the body will contain paragraphs and paragraphs of text (line).

+6
source share
9 answers

You can use regex to match string search in C #. You also have the opportunity to ignore the case.

 if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) 

This link may be useful: A practical guide. Finding Strings Using Regular Expressions (C # Programming Guide)

+5
source

I do not believe string has overloaded Contains with StringComparison . However, you can use IndexOf which does :

 if (body.IndexOf("software", StringComparison.CurrentCultureIgnoreCase) != -1) 
+7
source

I'm not sure if you are using .NET 1.1, but it does not contain the Contains method. You must use IndexOf ..NET 2.0 added method Contains (for MSDN) . With IndexOf, you can use StringComparison .

+6
source

String.Contains accepts only one parameter - your code should be

 bodyText.Contains("software"); 
+2
source

Contains has only one parameter - the string that it compares. Did you mean the Equals that StringComparison accepts?

+2
source

From the code that was inserted, you declare the "body" variable of the type string and use another "bodyText" variable that is not declared.

+1
source

The string does not have a Contains method with this signature. str.Contains (chr, StringComparison), not str.Contains (string, StringComparison) ...

+1
source

Well, you can always make an extension method (if you use .Net 3.5 or higher):

 public static class StringExtensions { /// <summary> /// Returns a value indicating whether the specified String object /// occurs within this string. /// </summary> /// <param name="str">string object being extended</param> /// <param name="value">string object to check for</param> /// <param name="comparer">StringComparer to use</param> public static bool Contains(this string str, string value, StringComparer comparer) { StringComparison comparison; if (comparer == StringComparer.CurrentCulture) comparison = StringComparison.CurrentCulture; else if (comparer == StringComparer.CurrentCultureIgnoreCase) comparison = StringComparison.CurrentCultureIgnoreCase; else if (comparer == StringComparer.InvariantCulture) comparison = StringComparison.InvariantCulture; else if (comparer == StringComparer.InvariantCultureIgnoreCase) comparison = StringComparison.InvariantCultureIgnoreCase; else if (comparer == StringComparer.Ordinal) comparison = StringComparison.Ordinal; else if (comparer == StringComparer.OrdinalIgnoreCase) comparison = StringComparison.OrdinalIgnoreCase; else comparison = StringComparison.Ordinal; if (str.IndexOf(value, comparison) != -1) return true; else return false; } } 
+1
source

You can still use Contains if you compare it after converting it to the same register (UPPER or lower)

eg:

 "samplestring".ToUpper().Contains("SAMPLESTRING") 
0
source

Source: https://habr.com/ru/post/893779/


All Articles