在 List<T> 中选择带有元音的单词 [英] Select words with vowels in List&lt;T&gt;

查看:58
本文介绍了在 List<T> 中选择带有元音的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 StreamReader 类从 txt 文件中读取并将其加载到列表中,现在我想选择带有元音的单词并将它们存储到新列表中,以便我可以使用 StreamWriter 只将选定的单词写入新的 txt 文件中

I use StreamReader Class to read from txt file and load it into List, now i want to select on words with vowels and store them into new List so i can use StreamWriter to write only selected words into new txt file

推荐答案

要从给定的单词列表(您检索的)中选择单词,您可以使用 Linq 选择包含元音的单词:

To select words from a given list of words (that you retrieve however) you can select the ones that only contain vowels using Linq:

string vowels = "aeiou";
List<string> words = new List<string>();
//words populated
var vowelWords = words.Where( word => word.All( c => vowels.Contains(c) ))
                      .ToList();

如果您只想要包含至少一个元音的单词(从问题中不太清楚):

If you just want words that contain at least a vowel (its not quite clear from the question):

var vowelWords = words.Where( word => word.Any( c => vowels.Contains(c) ))
                      .ToList();

根据评论进行

要选择包含多个元音的单词:

To select words that have more than one vowel:

var vowelWords = words.Where(word => word.Count(c => vowels.Contains(c)) > 1)
                      .ToList();

这篇关于在 List<T> 中选择带有元音的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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