c#-如何删除字符串数组中的元音

本文关键字:数组 字符串 何删除 删除 c#- | 更新日期: 2023-09-27 18:15:15

using System;    
using System.Collections.Generic;    
using System.Linq;   
using System.Text;    
using System.Threading.Tasks;
namespace myApp  
{    
    class Program
    {
        static void Main(string[] args)
        {
            string[] vowels = new string[]{"A","a","E","e","I","i","O","o","U","u"};
            for(int j=0;j<vowels.Length;j++)
            {
                string[] names = new string[5];
                names[0] = "john";
                names[1] = "samuel";
                names[2] = "kevin";
                names[3] = "steve";
                names[4] = "martyn";
            for (int i = 0; i < names.Length; i++)
            {
               if(vowels[j]==names[i])
               {
               }
            }

                Console.WriteLine("The output is:"+names[i]);
            }
                Console.ReadLine();
        }
    }
}

谁能帮我如何从给定名称中删除元音并在控制台中显示它们?

c#-如何删除字符串数组中的元音

您可以使用Linq

string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";
var vowels = new HashSet<char>("AaEeIioUu");

names = names.Select(Name => string.Concat(Name.Where(C => !vowels.Contains(C))))
             .ToArray();
Console.WriteLine(string.Join(Environment.NewLine, names));

可以使用正则表达式。替换:

Regex r = new Regex("[aAeEiIoOuU]");
//or Regex r = new Regex("[aeiou]", RegexOptions.IgnoreCase);
string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";
for (int i = 0; i < names.Length; i++)
{
    names[i] = r.Replace(names[i], "");
    Console.WriteLine("The output is:" + names[i]);
}

要使您的原始方法工作,您需要添加对string的调用。替换:

names[i] = names[i].Replace(vowels[j], "");

表示"替换names[i]中出现的任何vowels[j],并将结果赋值给names[i]"。

然而,你目前在你的元音循环中声明你的名字数组,所以如果你添加替换代码,你不会得到你期望的结果。

你还在元音和名字之间循环;从逻辑上讲,反向操作可能是有意义的——这当然使输出结果更容易。像这样的东西应该为您工作:

string[] vowels = new string[] { "A", "a", "E", "e", "I", "i", "O", "o", "U", "u" };
string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";
for (int i = 0; i < names.Length; i++)
{
    for (int j = 0; j < vowels.Length; j++)
    {
        names[i] = names[i].Replace(vowels[j], "");
    }
    Console.WriteLine("The output is:" + names[i]);
}

编辑

在评论中OP要求一个不使用Replace的例子。这里有一个这样的方法(@Eser在他们的回答中有另一个)。这种方法迭代输入字符串的每个字符,直到找到一个元音。此时,在此之前已读取的字符(不包括元音)被添加到StringBuilder:

public static string RemoveVowels(string name)
{
    StringBuilder noVowels = new StringBuilder();
    //keep track of the last index we read
    int lastIndex = 0;
    int i;
    for (i = 0; i < name.Length; i++)
    {
        if (vowels.Contains(name[i]))
        {
            //the current index is a vowel, take the text from the last read index to this index
            noVowels.Append(name, lastIndex, i - lastIndex);
            lastIndex = i + 1;
        }
    }
    if (lastIndex < i)
    {
        //the last character wasn't a vowel so we need to add the rest of the string here.
        noVowels.Append(name, lastIndex, name.Length - lastIndex);
    }
    return noVowels.ToString();
}

上面的方法可以对数组中的每个名字调用:

for (int i = 0; i < names.Length; i++)
{
    Console.WriteLine("The output is:" + RemoveVowels(names[i]));
}

至于使用哪一种方法,我会选择您认为最易读的方法,除非您有一些特定的性能需求,此时我认为您需要度量每种方法并选择最适合您需求的方法。

首先,如果要处理包含name的数组,则需要嵌套循环。你必须遍历你的名字,然后遍历每个元音。然后使用String。替换完成此过程。

name = name.Replace(vowels[j], String.Empty);

Eser的回答是最简洁和正确的方法,但如果您想更精细地控制何时删除哪些元音,您也可以尝试这样做:

string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";
List<char> vowels = new List<char>("AaEeIiOoUuYy".ToCharArray());
for(int i = 0; i < names.Length; i++) {
    string name = names[i];
    string trimmedName = name;
    foreach(char vowel in vowels) {
        int vowelIndex;
        while((vowelIndex = trimmedName.IndexOf(vowel)) != -1) {
            trimmedName = trimmedName.Substring(0, vowelIndex) + trimmedName.Substring(vowelIndex + 1);
        }
    }
    name = trimmedName;
}

这个更显式,性能更差,而且肯定更难看——所以您可能想要使用原始的解决方案。

我认为这是最简单的选择:

static void Main(string[] args)
{
    string[] names = new string[] { "john", "samuel", "george", "steve", "martyn" };
    foreach (var name in names)
    {
        string withoutVowels = new string(name.Where(x => !"aeiou".Contains(x)).ToArray());
        Console.WriteLine("The output is: " + withoutVowels);
    }
    Console.ReadKey();
}

如果您碰巧也需要大写,只需使用这一行:

string withoutVowels = new string(name.Where(x => "aeiou".IndexOf(x.ToString(), StringComparison.InvariantCultureIgnoreCase) < 0).ToArray());

当然,你也可以使用"aeiouAEIOU",并坚持使用第一个选项。

为了方便起见,非常简短的版本,基于Eser的回答:

static void Main(string[] args)
{
    string[] names = new string[] { "johnE", "samuel", "george", "steve", "martyn" };
    Console.WriteLine(string.Join(Environment.NewLine, names.Select(s => new string(s.Where(x => !"aeiou".Contains(x)).ToArray()))));
}
forech(string s in strings)
{
    s.Replace("a", "");
    // etc
}

您可以使用ToUpper/ToLower来检查元音,这样您就不必列出两次元音(每个大小写一次)。

首先遍历每个名字,然后从每个名字遍历每个元音。然后,使用.Replace()

删除匹配的元音。

下面是工作示例:

小提琴:https://dotnetfiddle.net/STnyWE

using System;    
public class Program
{
  public static void Main()
  {
    string[] vowels = new string[]{"A","E","I","O","U"};

            string[] names = new string[5];
            names[0] = "john";
            names[1] = "samuel";
            names[2] = "kevin";
            names[3] = "steve";
            names[4] = "martyn";
            for (int i = 0; i < names.Length; i++)
            {
                foreach(var v in vowels)
                {
                    if(names[i].ToString().ToUpper().Contains(v.ToString()))
                    {
                        Console.WriteLine(names[i]);
                        names[i] = names[i].ToString().ToUpper().Replace(v.ToString(), "");
                        Console.WriteLine("The output is: "+names[i].ToString().ToLower());
                    }
                }
            }
            Console.ReadLine();
   }
}