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();
}
}
}
任何人都可以帮助我如何从给定名称中删除元音并在控制台中显示它们吗?
答案 0 :(得分:2)
您可以使用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));
答案 1 :(得分:2)
您可以使用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], "");
说“在vowels[j]
中替换names[i]
的任何出现,并将结果分配给names[i]
”。
但是,您目前正在声明 in 元音循环中的名称数组,因此如果添加替换代码,您将无法获得预期的结果。
你也绕着元音然后是名字;从逻辑上讲,反转它可能是有意义的 - 这肯定会使输出结果更容易。这样的事情对你有用:
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在their answer中有另一种方法)。此方法迭代输入字符串的每个字符,直到找到元音。此时,直到那时读取的字符(不包括元音)被添加到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]));
}
关于使用哪种方法,我会选择你认为最具可读性的方法,除非你有一些特定的性能要求,我认为你需要测量每种方法并选择最符合你要求的方法
答案 2 :(得分:1)
首先,如果要处理名称数组,则需要嵌套循环。你必须遍历你的名字,然后循环通过每个元音。然后使用String.Replace来完成该过程。
name = name.Replace(vowels[j], String.Empty);
答案 3 :(得分:0)
我认为这是最简单的选择:
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()))));
}
答案 4 :(得分:0)
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;
}
这有点明确,性能较差,但肯定更难看 - 所以你可能想要使用原始解决方案。
答案 5 :(得分:-1)
forech(string s in strings)
{
s.Replace("a", "");
// etc
}
答案 6 :(得分:-1)
您可以使用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();
}
}