Remove Special Characters from a String

Write a program to remove all special char from given string.

For example:

Input:
India is Great!

Output:
India is Great

Solution:

using System;
using System.Text;

namespace Repeateed
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter String");
            var str = Console.ReadLine();

            Console.WriteLine(RemoveSpecialCharacters(str));
            Console.ReadLine();
        }

        static string RemoveSpecialCharacters(string str)
        {
            var sb = new StringBuilder();
            foreach (char c in str)
            {
                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *