Introduction
In this article we will learn how to remove unwanted character or words from a string in c#. Remove unwanted characters from a string using LINQ.
Previous Updates
In previous articles we have learnt Showing Chart With Database In Asp.Net Using C#. Maintain state of dynamic added userConrol in asp.net using c#.
Description:
I have faced one scenerio when any characters can appear in my input string, and I only want to keep the characters that I want (without messing up the order of course). So here is the best practice to remove that unwanted characters from string.
Remove Special Character from a String using Regex
class Program
{
static void Main(string[] args)
{
string filteredString = RemoveUnwantedCharacters("*73AB-#g@JP__0328hf014num98k",
"AB-0123456789@JP%-.".ToCharArray());
}
private static string RemoveUnwantedCharacters(string input,
char[] allowedCharacters)
{
var filtered = input.ToCharArray()
.Where(c => allowedCharacters.Contains(c))
.ToArray();
return new String(filtered);
}
}
|
Insert Only Numeric values in Asp Textbox using Regex.
Output:
In output it will returnall the characters which are pass as allowedCharacters and remove all other character from input .
73AB-@JP032801498 |
0 comments:
Post a Comment