2

In C# there is a a problem: there is no case insensitive String.Contains method (see Case insensitive 'Contains(string)').

In VB.NET the problem is the same, but there are a workaround:

Dim Str As String = "UPPERlower"
Dim b As Boolean = InStr(Str, "UpperLower") 

However, I have little "problems" with it :

1) In the Immediate Window of Visual Studio this method appear as "not declared";

2) How can I call this method dynamically (what should be the "caller" object) ? Say actually I should call it like this:

expr = Expression.Call(myStringParam, "Contains", Nothing, constantExpression, Expression.Constant(StringComparison.InvariantCultureIgnoreCase))

3) Where is located (who owns, what assembley) the InStr Function?

Community
  • 1
  • 1
serhio
  • 28,010
  • 62
  • 221
  • 374
  • What do you mean by "call this method dynamically" exactly? As for the immediate window, I suspect it can't handle extension methods, basically.... – Jon Skeet Sep 09 '11 at 13:34
  • @Jon Skeet: He means invoking it as an instance method. – BoltClock Sep 09 '11 at 13:35
  • @serhio: Right. Mentioning expression trees would have helped :) Have added an answer showing how to do this. – Jon Skeet Sep 09 '11 at 14:05

4 Answers4

8

(I see now that your question also deals with Expressions, and I do not have much experience with those specifically, but thankfully Jon Skeet may be able to help you with that. As for other parts of your problem, my original answer is below.)


InStr exists inside Microsoft.VisualBasic.Strings. An example of invoking it from C# code

string myString = "Hello World";
int position = Microsoft.VisualBasic.Strings.InStr(myString, "world");

Of course, if I wanted a case-insensitive result, I would opt for the overload of IndexOf that exists on System.String that allows me to specify a StringComparison.

int index = myString.IndexOf("world", StringComparison.CurrentCultureIgnoreCase);

Also note that InStr starts at 1 for items found, and IndexOf starts at 0 for such items. index != position in this code snippet.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
2

The immediate window of Visual Studio is a bit different than normal execution of the method from a compiled program. It's little different than executing the extension method under the debugger (really it's the same).

This was not well supported in Visual Studio 2005 or 2008. There's not a lot that can be done there to make it work other than call it by it's non-extension method form

TheModule.Contains(source, toTest)

The support for it is much improved in 2010 and I would expect it to work.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

Having seen your edit, it makes more sense - you can't call an extension method like that. The extension method is just a static method, so call it that way. For example:

expr = Expression.Call(GetType(MyExtensions), "Contains", Nothing, _
           myStringParam, constantExpression, _
           Expression.Constant(StringComparison.InvariantCultureIgnoreCase))

where MyExtensions is the class declaring the Contains extension method.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks, Jon. Remark, in VB.NEt the things are a little more complicated... because an extension method can be declared in a Module, not just a shared class... Hope will work with a module too... – serhio Sep 09 '11 at 14:06
  • @serhio: I believe a module will be compiled to a static class anyway - look in Reflector. – Jon Skeet Sep 09 '11 at 14:16
  • apropos, what about `Microsoft.VisualBasic.Strings.InStr` on that chapter? I suppose the same thing but `GetType(Microsoft.VisualBasic.Strings)`... @Jon Skeet: works very well with module, a propos ;) – serhio Sep 09 '11 at 14:19
  • @serhio: Yes, you could probably do the same thing with `InStr`. – Jon Skeet Sep 09 '11 at 14:21
1

You can take Anthony's suggestion and make an extension method (or just a normal method) and use IndexOf:

<Extension()>
Public Shared Function CaseInsensitiveContains(source As String, value As String) As Boolean
    Return source.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0
End Function
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48