String remove non alphanumeric
The following asp.net c# example code demonstrate us how can we remove non alphanumeric characters
programmatically at run time from a string object. So, we keep only alphabet (letter) (a to z) and numeric characters
(0 to 9). .Net framework's String class has no built in method or property to remove non alphanumeric characters
from a string value.
To remove non alphanumeric characters from a string object, first we need to convert the string value to a char array. String.ToCharArray() copies the characters in this instance to a Unicode character array. Next, we need to loop through the char array elements. In this example, we loop char array elements by using foreach loop.
Finally, we can test, which character is alphanumeric or not. We can determine a character's alphanumeric state by using Char.IsLetterOrDigit() method. The Char.IsLetterOrDigit(Char) overloaded method indicate whether a Unicode character is categorized as a letter or a decimal digit. This way we can check each character and remove non alphanumeric character from string value.
To remove non alphanumeric characters from a string object, first we need to convert the string value to a char array. String.ToCharArray() copies the characters in this instance to a Unicode character array. Next, we need to loop through the char array elements. In this example, we loop char array elements by using foreach loop.
Finally, we can test, which character is alphanumeric or not. We can determine a character's alphanumeric state by using Char.IsLetterOrDigit() method. The Char.IsLetterOrDigit(Char) overloaded method indicate whether a Unicode character is categorized as a letter or a decimal digit. This way we can check each character and remove non alphanumeric character from string value.
string-remove-non-alphanumeric.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//this section create a string variable.
string characters = "1.{-A}* 2.[B)";
Label1.Text = "string of characters..................<br />";
Label1.Text += characters + "<br /><br />";
//this line convert string to char array
char[] chars = characters.ToCharArray();
characters = "";
foreach(char c in chars)
{
if (char.IsLetterOrDigit(c))
{
Label1.Text += c.ToString();
characters += c;
}
else
{
Label1.Text += "non alphanumeric character";
}
Label1.Text += "<br />";
}
Label1.Text += "<br />after removing non alpha numeric characters from string...<br />";
Label1.Text += characters;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string remove non alphanumeric</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string remove non alphanumeric
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="string remove non alphanumeric"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to format a decimal string as percentage value
- How to format a string as hexadecimal
- How to convert an integer to fixed length hex string format
- How to format a string with leading zeros
- How to format a string to a fixed length string
- How to remove a character from a string at specified position
- How to remove a substring from a string
- How to remove special characters from a string
- How to remove non numeric characters from a string
- How to reverse a string