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