winforms 删除名称中的所有元音

yptwkmov  于 2022-12-30  发布在  其他
关注(0)|答案(9)|浏览(155)

我正在编写一个Windows窗体应用程序,其中用户可以指定一个名称,程序将返回不带元音的名称。但是程序如何理解如果名称包含A、E、I、O、U、Y,则应删除这些字母呢?

qni6mghb

qni6mghb1#

只需要去掉所有的元音字母(大写字母也一样),然后重新赋给这个名字:

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

mwg9r5ms2#

我知道这是一个较老的线程,但这里有一个稍微干净/更健壮的方法来完成这一点与正则表达式。

string name = "Some Name with vowels";

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

qgzx9mmu3#

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

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

esyap4oy4#

首先创建一个扩展方法来标识元音,您可以在任何需要的地方重用该方法:

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
2nbm6dog

2nbm6dog5#

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

kzipqqlq6#

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

lztngnrs7#

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
        }
xiozqbni

xiozqbni8#

在文本框和正则表达式中使用字符串:使用系统;

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

        }
    }
3qpi33ja

3qpi33ja9#

string str1 = "Hello World";

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

Console.WriteLine(str);

Console.Read();

相关问题