Powered by Blogger.

Sunday, February 16, 2014

Get only alphabet characters from string in asp.net

In this post we will discuss how to get only alphabet characters from a string in asp.net using Regular Expressions.

Also you can check out my previous posts on:

- Bind dropdownlist from enum in Asp.Net

- NuGet tutorial in Asp.Net

- Automated Performance Testing and its Parameters

To retrieve alphabet characters from string we can use a RegularExpressions.

Below is the full code:

First we have the using statement like below:

using System.Text.RegularExpressions;

.aspx code:

 Input String: <asp:Label ID="lblInput" runat="server" Text="qa1rb0h)g(2d#edgtkhrint98@#09&&*_---+=!~123Avcf"></asp:Label><br />
        <asp:Button ID="btnGetAlphabets" runat="server" Text="Get Alphabets" OnClick="btnGetAlphabets_Click" /><br />
        <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>

.cs code:
  protected void btnGetAlphabets_Click(object sender, EventArgs e)
        Regex reg = new Regex(@"[^a-zA-Z]");

        string result = reg.Replace(lblInput.Text, "");

        lblResult.Text ="Result:  "+ result;

The result will come like below: