Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using Contains with empty string
Message
From
18/07/2014 09:07:31
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01604011
Message ID:
01604015
Views:
45
>Hi,
>
>I need to check if a string is within another string. Here is an example:
>
>
> if (!LongString.Contains(ShortString))
>    {
>           // do something
>    }
>
>
>The problem I have is when the short string (ShortString) is empty (""), "do something" never does anything. Of course I can get around the problem by adding to the IF ShortString.Length = 0 || . But I was wondering, can I change the .Contains somehow to make it work with empty string?

Nothing you can do about how 'Contains' works. Maybe :
if (string.IsNullOrEmpty(ShortString) || !LongString.Contains(ShortString))
or you could get fancy and write your own extension method :-}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform