w3resource

C#: Remove all vowels from a given string


Remove Vowels from String

Write a C# Sharp program to remove all vowels from a given string.

Sample Solution:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declaring a string variable
            string text;

            // Testing the function test with different input strings

            // String containing only letters (Python)
            text = "Python";
            Console.WriteLine("Orginal string: " + text);
            Console.WriteLine("After removing all the vowels from the said string: " + test(text));

            // String containing letters and a space (C Sharp)
            text = "C Sharp";
            Console.WriteLine("\nOrginal string: " + text);
            Console.WriteLine("After removing all the vowels from the said string: " + test(text));

            // String containing only letters (JavaScript)
            text = "JavaScript";
            Console.WriteLine("\nOrginal string: " + text);
            Console.WriteLine("After removing all the vowels from the said string: " + test(text));
        }

        // Method to remove all vowels from a string using Regex
        public static string test(string text)
        {
            // Using Regex.Replace to remove vowels (lowercase and uppercase) from the input string
            return new Regex(@"[aeiouAEIOU]").Replace(text, "");
        }
    }
}

Sample Output:

Orginal string: Python
After removing all the vowels from the said string: Pythn

Orginal string: C Sharp
After removing all the vowels from the said string: C Shrp

Orginal string: JavaScript
After removing all the vowels from the said string: JvScrpt

Flowchart:

Flowchart: C# Sharp Exercises - Remove all vowels from a given string.

For more Practice: Solve these Related Problems:

  • Write a C# program to trim a set of custom symbols and whitespace from both ends of a string using only character arrays.
  • Write a C# program to remove trailing punctuation marks from a sentence without using predefined trim methods.
  • Write a C# program to repeatedly trim nested brackets from both ends of a string until no more brackets remain.
  • Write a C# program to strip surrounding digits from a string until only alphabetic characters are left.

Go to:


PREV : Remove Non-Letter Characters.
NEXT : Indices of Lowercase Letters.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.