C#/.NET Little Wonders: Searching Strings With Contains(), StartsWith(

added by BlackRabbitCoder
10/14/2011 10:17:29 AM

122 Views

Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. Two weeks ago I decided to stop my Little Wonders in the String class, but I recanted and decided to do one more before wrapping up String.  So today we’ll look at the many different ways to find a string inside of another string using IndexOf(), LastIndexOf(), Contains(), StartsWith(), and EndsWith().


3 comments

dpeterson
10/15/2011 9:37:20 PM
Is there a performance/ease-of-use trade-off between these string functions and regular expressions? I would assume that for short strings the compilation time for a regular expression would counteract its speed.

bradygaster
10/17/2011 6:13:59 AM
Good question. I've used both, and admit I find myself falling into using the string native methods more often than not when I'm doing more simple tricks.

BlackRabbitCoder
10/17/2011 9:36:40 AM
I think that's one of those things where I tend to favor the side of maintainability until it becomes a known performance bottleneck. I do mostly server-side processing (WCF, windows services, etc) so most of the time the DB calls or web service calls tend to make any other inefficiency pale in comparison.

I'd imagine, as in all things, it would depend a lot on the complexity of the expression, length of string, etc.