从名称中删除所有元音

时间:2011-04-15 12:29:54

标签: c# winforms

我正在编写一个Windows窗体应用程序,用户可以在其中指定名称,程序将返回没有人声的名称。但程序如何理解如果名称包含A,E,I,O,U,Y,那么这些字母将被删除。

9 个答案:

答案 0 :(得分:12)

只需删除所有元音(大写相同)并再次将其指定给名称:

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

答案 1 :(得分:3)

其他人可能会提供一个正则表达式的例子,但我会考虑直截了当的方法:

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);

答案 2 :(得分:2)

首先创建一个扩展方法来识别可以在任何需要的地方重复使用的元音:

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);
  }
}

然后就像这样使用它

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

答案 3 :(得分:2)

我知道这是一个较老的帖子,但这里有一个稍微更清晰/更健壮的方法来用正则表达式完成这个。

string name = "Some Name with vowels";

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

答案 4 :(得分:0)

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
        }

答案 5 :(得分:0)

在文本框和正则表达式中使用字符串: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);
        }

        }
    }

答案 6 :(得分:0)

//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();

答案 7 :(得分:0)

   static string RemoveVowel(string input)
    {
        char[] vowels = new char[] { 'a', 'e' ,'i' ,'o', 'u' };
        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();
    }

答案 8 :(得分:0)

string str1 = "Hello World";

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

Console.WriteLine(str);

Console.Read();