gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:05:58 24 4
gpt4 key购买 nike

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

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

最佳答案

你可以使用 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));

关于c#-如何删除字符串数组中的元音字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32871261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com