How to Get Only Letters from a String in C#?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Your String.");
            string myString = Console.ReadLine();
            var onlyLetters = new String(myString.Where(Char.IsLetter).ToArray());
            Console.WriteLine(onlyLetters);
            Console.Read();
        }
    }
}

Here, the Final Output is shown in the following image. Here output is According to our Input String as Uppercase and Lowercase.

Final Output

If we want only Print Uppercase(Capital Letter) only that time we change in condition Char.IsUpper (c)).ToArray().

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter Your String.");
        string myString = Console.ReadLine();
        var onlyLetters = new String(myString.Where(c => Char.IsLetter(c) && Char.IsUpper(c)).ToArray());
        Console.WriteLine(onlyLetters);
        Console.Read();
    }
}

The final output is shown in the following image.

String Output

3-You can achieve this using Regex.Replace. Firstly add a namespace using System.Text.RegularExpressions.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter Your String.");
        string myString = Console.ReadLine();
        // var onlyLetters = new String(myString.Where(Char.IsLetter).ToArray());
        string newmysring = Regex.Replace(myString, "[^a-zA-Z]", "");
        Console.WriteLine(newmysring);
        Console.Read();
    }
}

Here Final Output is shown in the following image.

Here Final Output

4- C# program that prints alternative letters of a given string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace String1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Your String.");
            string myString = Console.ReadLine();
            string alternativeLetters = "";

            for (int i = 0; i < myString.Length; i += 2)
            {
                alternativeLetters += myString[i];
            }

            Console.WriteLine(alternativeLetters);
            Console.Read();
        }
    }
}

Here Final Output is shown in the following image.

 LINQ

Alternatively, you can use LINQ to achieve the same result in a more concise way.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter Your String.");
        string myString = Console.ReadLine();
        string alternativeLetters = new string(myString.Where((c, i) => i % 2 == 0).ToArray());
        Console.WriteLine(alternativeLetters);
        Console.Read();
    }
}

The final output is shown in the following image.

LINQ expression

This LINQ expression uses the `Where` method to select characters at even indices (0, 2, 4, ...) and then converts the resulting sequence back to a string.


Similar Articles