in

ASP.NET Weblogs

Dave Burke - A freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

StartsWith vrs. Substring(0,#)

I revived an old .ASP function in C# to pass a clean phone number to the DL and happened upon String().StartsWith, a superior method to Substring(0,Some_Int):

Lame

if (phonenum.Substring(0,2) == "1-" || phonenum.Substring(0,2) == "1." || phonenum.Substring(0,2) == "1 " || phonenum.Substring(0,2) == "1(")

Better

if (phonenum.StartsWith("1-") || phonenum.StartsWith("1.") || phonenum.StartsWith("1 ") || phonenum.StartsWith("1("))

Best

Some regex expression... :-)

 

Published Mar 26 2004, 10:51 PM by daveburke
Filed under:

Comments

 

Jerry Pisk said:

I doubt that regex would be faster than four String.StartsWith calls. It will still have to do the comparisons but it will add all the overhead of regexs, parsing it and all...
March 27, 2004 1:57 PM

Leave a Comment

(required)  
(optional)
(required)  
Add