开发者

Remove all vowels from a name

I am coding a Windows Forms application where the user can specify a name and the program will return the name without the vocals. But how does the program understand that if the name contains A, E, I, O, U, Y, then the letters s开发者_如何学JAVAhall be removed.


Just remove all the vowels (same for upper case) and assign it to the name again:

string vowels = "aeiouy";
string name = "Some Name with vowels";
name = new string(name.Where(c => !vowels.Contains(c)).ToArray());


I know this is an older thread but here is a slightly cleaner / more robust way of accomplishing this with a regular expression.

string name = "Some Name with vowels";

string output = Regex.Replace(name ,"[aeiou]", "", RegexOptions.IgnoreCase);


Somebody else will probably offer a regex example, but I would consider a straight forward approach to this:

string name = "Flintstone";
     string output = name.Replace("a", string.Empty)
                              .Replace("e", string.Empty)
                              .Replace("i", string.Empty)
                              .Replace("o", string.Empty)
                              .Replace("u", string.Empty)
                              .Replace("y", string.Empty);


First create a extension method to indentify a vowel that you can reuse wherever you need:

public static class MyExtensions{
  public static bool IsVowel( this char c ){
    return new[]{ 'a','e','i','o','u','y','A','E','I','O','U','Y' }.Contains(c);
  }
}

Then simply use it like this

string test = "Hello how are u";    
string result = new string(test.Where( c => !c.IsVowel() ).ToArray()); //result is Hll hw r 


//remove vowels in string in C#

string s = "saravanan";

string res = "";

char[] ch = { 'a', 'e', 'i', 'o', 'u' } ;

foreach (char c in ch)
{
    for (int i = 0; i < s.Length; i++)
    {

        if (s[i] == c)
        {
            res = res + "";
        }
        else
        {
            res = res + s[i];
        }
    }
    break;
}

Console.WriteLine(res);
Console.ReadLine();


   static string RemoveVowel(string input)
    {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < input.Length; i++)
        {
            switch (input[i])
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    sb.Append("");
                    break;

                default:
                    sb.Append(input[i]);
                    break;
            }
        }
         return sb.ToString();
    }


static void Main()
        {
            //using HashSet
            //ExceptWith removes the specified elements from the source set. Here, we strip all
            //vowels from the set:

            var letters = new HashSet<char>("Mark");
            letters.ExceptWith("aeiou");
            foreach (char c in letters) Console.Write(c);     // Mrk
        }


Using string in a textbox and regular expression:using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btndeletevowel_Click(object sender, EventArgs e)
        {
            string s1;
            string s2;
            s1 = textBox1.Text;
            s2 = System.Text.RegularExpressions.Regex.Replace(s1, "[aeiouAEIOU]", "");
            MessageBox.Show(s2);
        }

        }
    }


string str1 = "Hello World";

string str = Regex.Replace(str1, "[aeiouAEIOU]", " ");

Console.WriteLine(str);

Console.Read();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜