将字符串拆分为基于长度的单词列表C# [英] Splitting string into words length-based lists c#
问题描述
我有一串用空格隔开的单词.如何根据单词长度将字符串分成单词列表?
I have a string of words separated by spaces. How to split the string into lists of words based on the words length?
示例
输入:
" aa aaa aaaa bb bbb bbbb cc ccc cccc cccc bbb bb aa "
输出:
List 1 = { aa, bb, cc}
List 2 = { aaa, bbb, ccc}
List 3 = { aaaa, bbbb, cccc}
推荐答案
编辑:很高兴我的原始答案帮助OP解决了他们的问题.但是,在考虑了一下问题之后,我对其进行了调整(我强烈建议反对我以前的解决方案,我将其留在了帖子的结尾).
I'm glad my original answer helped the OP solve their problem. However, after pondering the problem a bit, I've adapted it (and I strongly advise against my former solution, which I have left at the end of the post).
一种简单的方法
string input = " aa aaa aaaa bb bbb bbbb cc ccc cccc cccc bbb bb aa ";
var words = input.Trim().Split().Distinct();
var lookup = words.ToLookup(word => word.Length);
说明
首先,我们修剪输入以避免外部空间中的空白元素.然后,我们将字符串分成一个数组.如果单词之间的多个空格 ,则需要像 Mark的答案一样使用StringSplitOptions
.
Explanation
First, we trim the input to avoid empty elements from the outer spaces. Then, we split the string into an array. If multiple spaces occur in between the words, you'd need to use StringSplitOptions
as as in Mark's answer.
在调用Distinct
仅包含每个单词一次之后,我们现在将words
从IEnumerable<string>
转换为Lookup<int, string>
,其中单词的长度由键(int)
表示,单词本身存储在值(string)
.
After calling Distinct
to only include each word once, we now convert words
from IEnumerable<string>
to Lookup<int, string>
, where the words' length is represented by the key (int)
and the words themselves are stored in the value (string)
.
等等,那怎么可能?每个键我们都没有多个单词吗?当然可以,但这正是 Lookup
类的作用: /p>
Hang on, how is that even possible? Don't we have multiple words for each key? Sure, but that's exactly what the Lookup
class is there for:
Lookup<TKey, TElement>
表示一组键,每个键都映射到一个或多个 价值观.Lookup<TKey, TElement>
类似于Dictionary<TKey, TValue>
.区别在于 字典将键映射到单个值,而查找 将键映射到值集合.
Lookup<TKey, TElement>
represents a collection of keys each mapped to one or more values. ALookup<TKey, TElement>
resembles aDictionary<TKey, TValue>
. The difference is that a Dictionary maps keys to single values, whereas a Lookup maps keys to collections of values.
您可以通过在对象上调用ToLookup
来创建Lookup
的实例
实现IEnumerable<T>
.
You can create an instance of a Lookup
by calling ToLookup
on an object
that implements IEnumerable<T>
.
注意
没有公共构造函数来创建Lookup的新实例.
此外,查找对象是不可变的,也就是说,您不能添加或
创建查找后,从查找中删除元素或键.
Note
There is no public constructor to create a new instance of a Lookup.
Additionally, Lookup objects are immutable, that is, you cannot add or
remove elements or keys from a Lookup after it has been created.
word => word.Length
是 KeySelector lambda:它定义了我们要通过单词的长度来索引(或分组,如果可以的话)Lookup
.
word => word.Length
is the KeySelector lambda: it defines that we want to index (or group, if you will) the Lookup
by the Length of the words.
(类似于问题最初请求的输出)
foreach (var grouping in lookup)
{
Console.WriteLine("{0}: {1}", grouping.Key, string.Join(", ", grouping));
}
输出
2: aa, bb, cc
3: aaa, bbb, ccc
4: aaaa, bbbb, cccc
将一定长度的所有单词放入List
List<String> list3 = lookup[3].ToList();
按键订购
(请注意,这些将返回IOrderedEnumerable<T>
,因此无法再通过密钥进行访问)
Order by key
(note that these will return IOrderedEnumerable<T>
, so access by key is no longer possible)
var orderedAscending = lookup.OrderBy(grouping => grouping.Key);
var orderedDescending = lookup.OrderByDescending(grouping => grouping.Key);
原始答案-请不要这样做(性能不佳,代码混乱):
Original answer - please don't do this (bad performance, code clutter):
string input = " aa aaa aaaa bb bbb bbbb cc ccc cccc cccc bbb bb aa ";
Dictionary<int, string[]> results = new Dictionary<int, string[]>();
var grouped = input.Trim().Split().Distinct().GroupBy(s => s.Length)
.OrderBy(g => g.Key); // or: OrderByDescending(g => g.Key);
foreach (var grouping in grouped)
{
results.Add(grouping.Key, grouping.ToArray());
}
这篇关于将字符串拆分为基于长度的单词列表C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!