0
 comp = StringComparison.OrdinalIgnoreCase Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp)) 

That seems like the way. However, I tried and it doesn't work. It seems that the only altenative of s.Contains take char() as first argument. If I want to insert string as first argument then I cannot have second argument as StringComparison.OrdinalIgnoreCase.

I got the snippet from https://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.110).aspx

0

2 Answers 2

3

You can use IndexOf function.

Console.WriteLine(s.IndexOf(sub1, 0, StringComparison. OrdinalIgnoreCase) > -1) 

Above snippet prints true or false depending on whether s contains sub1.

Alternatively, if you want to use contains, you can use ToUpper or ToLower as shown below...

Console.WriteLine(" {0:G}: {1}", comp, s.ToUpper().Contains(sub1.ToUpper())) 

The link you gave in question shows you how to create a custom extension to string. He also gave the code you have to use to create that custom extension and that method uses IndexOf internally if you had observed.

Source

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

2 Comments

Hmmm but microsoft sample says we can use string.contains.
@JimThio No, it says "you can create a custom method..."
1

Try this:

s.ToLowerInvariant().Contains(sub1.ToLowerInvariant()) 

The link you pointed to in your question defines the follow extension method:

Imports System.Runtime.CompilerServices Module StringExtensions <Extension()> Public Function Contains(str As String, substring As String, comp As StringComparison) As Boolean If substring Is Nothing Then Throw New ArgumentNullException("substring", "substring cannot be null.") Else If Not [Enum].IsDefined(GetType(StringComparison), comp) Throw New ArgumentException("comp is not a member of StringComparison", "comp") End If Return str.IndexOf(substring, comp) >= 0 End Function End Module 

Unless you implement this your call to s.Contains(sub1, comp) won't work.

1 Comment

By the way, those tests are really redundant, because IndexOf already throws the same exceptions when the arguments are bad. See msdn.microsoft.com/en-us/library/ms224425%28v=vs.110%29.aspx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.