开发者

String Contains IgnoreCase in VB.NET

开发者 https://www.devze.com 2023-04-04 06:37 出处:网络
In C# there is a a problem: there is no case insensitive String.Contains method (see Case insensitive 'Contains(string)').

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 thi开发者_如何学Pythons 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?


(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.


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号