Skip to main content

Command Palette

Search for a command to run...

Removing vowels from a string in C#

Updated
2 min read
Removing vowels from a string in C#
H

Looking to get into software development? As a software engineer I will guide you on this journey and give you bite sized tips every single day 👊

Hello! In this short tutorial, we will see a quick solution to remove all vowels from a string in C# with linq. The idea is to delete all vowels from the string.

Remove all vowels in C

So, let's dive into this solution. The first thing that we will do is to create a console application in visual studio and then create a string called vowels in the main function.

using System;
using System.Linq;

namespace Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string vowels = "aeiouy";

            Console.ReadLine();
        }
    }
}

Now, that we create this string, let's create a simple text :

string text = "This is a text that contains vowels";

Now, the goal is to replace all vowels with spaces.

With Linq, we can do it quickly as follows:

text = new string(text.Where(c => !vowels.Contains(c)).ToArray());

Here we use ToArray to convert the IEnumerable to an array then we convert it to a string.

The output of this code will be as follows:

Ths s txt tht cntns vwls

The whole code :

using System;
using System.Linq;
namespace Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string vowels = "aeiouy";
            string text = "This is a text that contains vowels";
            text = new string(text.Where(c =>!vowels.Contains(c)).ToArray());
            Console.WriteLine(text);
            Console.ReadLine();
        }
    }
}

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Twitter

Software development in C# & .NET

Part 6 of 12

In this series, I will provide different articles on software engineering in C# and .NET framework. Different blog posts on C# programming will be provided each day.

Up next

A quick tutorial to learn delegates in C#

Learn delegates in C# with a detailed example !

Removing vowels from a string in C#