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

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

任何人都可以帮助我如何从给定的名称中删除元音并在控制台中显示它们?

Eser的答案是最简洁和正确的方法,但是如果您想要更精细地控制何时以及要删除的元音,您还可以尝试以下方法:

 string[] names = new string[5]; names[0] = "john"; names[1] = "samuel"; names[2] = "kevin"; names[3] = "steve"; names[4] = "martyn"; List vowels = new List("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; } 

这有点明确,性能较差,但肯定更难看 - 所以你可能想要使用原始解决方案。

您可以使用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("AaEeIioUu"); names = names.Select(Name => string.Concat(Name.Where(C => !vowels.Contains(C)))) .ToArray(); Console.WriteLine(string.Join(Environment.NewLine, names)); 

你可以使用Regex.Replace :

 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.Replace的调用:

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

至于使用哪种方法,我会选择你认为最具可读性的方法,除非你有一些特定的性能要求,我认为你需要测量每种方法并选择最符合你要求的方法。

首先,如果要处理名称数组,则需要嵌套循环。 你必须遍历你的名字,然后循环通过每个元音。 然后使用String.Replace来完成该过程。

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

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

 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来检查元音,这样您就不必两次列出元音(每个shell一次)。

首先遍历每个名​​称,然后从每个名称循环遍历每个元音。 然后,使用.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(); } }