Search This Blog

Tuesday 3 February 2015

Remove Special Character from string in C#?

Step 1 : - Add the below method in your Default.aspx.cs file

 public string RemoveSpecialChars(string str)
    {
        string[] chars = new string[] { "\"""\\""/"":""*""?""<"">""|""\r""\n", };
        for (int i = 0; i < chars.Length; i++)
        {
            if (str.Contains(chars[i]))
            {
                str = str.Replace(chars[i], "").Trim();
            }
        }
        return str;

    }


Step 2 : - Calling the method.

        string StrSpecialChars = "A\\ B/ C: D? E F";

        string StrafterRemovingSpecialChars = RemoveSpecialChars(StrSpecialChars);

        Response.Write(StrafterRemovingSpecialChars);


No comments:

Post a Comment