C#中 - 如何删除元音字符串数组? [英] c#-how to delete vowels in an array of string?

查看:127
本文介绍了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();
        }
    }
}



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

can anyone help me how to delete the vowels from the given names and display them in console?

推荐答案

埃泽尔的答案是这样做的最简洁和正确的方法,但如果你想在和元音你删除,你也可以尝试像过更精细的控制:

Eser's answer is the most succinct and correct way to do this, but in case you want to more fine-grain control over when and which vowels you're removing, you could also try something like:

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

这是一个比较明确的,更少的高性能,并且肯定更难看,但 - - 所以你可能想要去与原来的解决方案

This is a bit more explicit, less performant, and definitely more ugly though -- so you may want to go with the original solution.

这篇关于C#中 - 如何删除元音字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆