Useful Extension Methods - Part 1 of n

28. August 2010

    Extension methods let you add new features to existing classes, even classes that you didn't write yourself. You can use them to add new methods to your own types, types written by others including basic data types.

In this series, I Will be posting some useful extension methods for the all the types. To begin with I am providing extension methods for strings.

String Extension Methods -1:

IsValidUrl

Checks if the string is a valid URL

public static bool IsValidUrl(this string input) 

{

    Regex rx = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");

    return rx.IsMatch(input);

}

//Usage

"http://elangovanr.com".IsValidUrl();

 

IsValidEmailAddress

Checks if the input string is a valid email address

public static bool IsValidEmailAddress(this string input)

{

    Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");

    return regex.IsMatch(input);

}

 

//Usage

"admin@elangovanr.com".IsValidEmailAddress();

 

IsNotNullOrEmpty

Checks if the input is not null or empty, negation of IsNullOrEmpty

public static bool IsNotNullOrEmpty(this string input) 

{

    return !String.IsNullOrEmpty(input);

}

 

//Usage

"test".IsNotNullOrEmpty();

 

ToBase64

Converts the string to base 64 encoded string

static public string ToBase64(this string input)

{

    byte[] toEncodeAsBytes

        = System.Text.ASCIIEncoding.ASCII.GetBytes(input);

    string returnValue

        = System.Convert.ToBase64String(toEncodeAsBytes);

    return returnValue;

}

 

//Usage

"test".ToBase64();

 

FromBase64

Decodes base 64 encoded string

static public string FromBase64(string input)

{

    byte[] encodedDataAsBytes

      = System.Convert.FromBase64String(input);

    string returnValue =

    System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

    return returnValue;

}

 

//Usage

"test".FromBase64();

 

IsValidIPAddress

Checks if the given string is a valid IP Address

public static bool IsValidIPAddress(this string s)

{

    return Regex.IsMatch(s, 

            @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b");

}

 

//Usage

"test".IsValidIPAddress();

 

ToHTMLEncoded

Encodes the input string as HTML (converts special characters to entities)

public static string ToHTMLEncoded(this string input)

{

    return HttpContext.Current.Server.HtmlEncode(input);

}

 

//Usage

"<span>test</span>".ToHTMLEncoded();

 

ToURLEncoded

Encodes the input string as a URL (converts special characters to % codes)

public static string ToURLEncoded(this string input)

{

    return HttpContext.Current.Server.UrlEncode(input);

}

 

//Usage

"http://elangovanr.com/?q=extension&page=2".ToURLEncoded();

 

HTMLDecoded

Decodes the HTML Encoded string

public static string HTMLDecoded(this string input)

{

    return HttpContext.Current.Server.HtmlDecode(input);

}

 

//Usage

"test".HTMLDecoded();

 

URLDecoded

Decodes the HTML Encoded string

public static string URLDecoded(this string input)

{

    return HttpContext.Current.Server.UrlDecode(input);

}

 

//Usage

"test".URLDecoded();

 

StripHTML

Removes any HTML tags from the input string

public static string StripHTML(this string input)

{

    return Regex.Replace(input, @"<(style|script)[^<>]*>.*?</\1>|</?[a-z][a-z0-9]*[^<>]*>|<!--.*?-->", "");

}

 

//Usage

"<span>test</span>".URLDecoded();

.Net Tips, Technical, c# ,

blog comments powered by Disqus