remove all vowels in a string using a regular expression in csharp

main.cs
string input = "Hello, World!";
string output = Regex.Replace(input, "[aeiouAEIOU]", "");
Console.WriteLine(output); // Hll, Wrld!
131 chars
4 lines

Explanation:

  • We start by defining the input string that we want to remove vowels from.
  • Next, we use the Regex.Replace() method to replace any vowels (lowercase or uppercase) with an empty string.
  • The first parameter of Regex.Replace() is the input string that we want to operate on.
  • The second parameter is the regular expression pattern that we want to match. In this case, we use the character class [aeiouAEIOU] to match any vowel (lowercase or uppercase).
  • The third parameter is the string that we want to replace any matches with, which in this case is an empty string.
  • Finally, we print out the resulting output string, which should be the original string with all vowels removed.

gistlibby LogSnag