C# 语法 - 通过逗号将字符串拆分为数组、转换为泛型列表和逆序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/315358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:53:11  来源:igfitidea点击:

C# Syntax - Split String into Array by Comma, Convert To Generic List, and Reverse Order

c#genericssyntaxilist

提问by BuddyJoe

What is the correct syntax for this:

什么是正确的语法:

IList<string> names = "Tom,Scott,Bob".Split(',').ToList<string>().Reverse();

What am I messing up? What does TSource mean?

我在捣乱什么?TSource 是什么意思?

采纳答案by Jon Skeet

The problem is that you're calling List<T>.Reverse()which returns void.

问题是您正在调用List<T>.Reverse()which 返回void.

You could either do:

你可以这样做:

List<string> names = "Tom,Scott,Bob".Split(',').ToList<string>();
names.Reverse();

or:

或者:

IList<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList<string>();

The latter is more expensive, as reversing an arbitrary IEnumerable<T>involves buffering all of the data and then yielding it all - whereas List<T>can do all the reversing "in-place". (The difference here is that it's calling the Enumerable.Reverse<T>()extension method, instead of the List<T>.Reverse()instance method.)

后者更昂贵,因为反转任意IEnumerable<T>数据涉及缓冲所有数据然后全部产生 - 而List<T>可以“就地”完成所有反转。(这里的区别在于它调用的是Enumerable.Reverse<T>()扩展方法,而不是List<T>.Reverse()实例方法。)

More efficient yet, you could use:

更高效,您可以使用:

string[] namesArray = "Tom,Scott,Bob".Split(',');
List<string> namesList = new List<string>(namesArray.Length);
namesList.AddRange(namesArray);
namesList.Reverse();

This avoids creating any buffers of an inappropriate size - at the cost of taking four statements where one will do... As ever, weigh up readability against performance in the real use case.

这避免了创建任何大小不合适的缓冲区 - 代价是在一个人可以做的地方执行四个语句......一如既往,在实际用例中权衡可读性与性能。

回答by Yona

Try this:

尝试这个:

List<string> names = new List<string>("Tom,Scott,Bob".Split(','));
names.Reverse();

回答by Rune Grimstad

List<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList();

This one works.

这个有效。

回答by JaredPar

What your missing here is that .Reverse() is a void method. It's not possible to assign the result of .Reverse() to a variable. You can however alter the order to use Enumerable.Reverse() and get your result

你在这里缺少的是 .Reverse() 是一个 void 方法。无法将 .Reverse() 的结果分配给变量。但是,您可以更改顺序以使用 Enumerable.Reverse() 并获得结果

var x = "Tom,Scott,Bob".Split(',').Reverse().ToList<string>()

The difference is that Enumerable.Reverse() returns an IEnumerable<T> instead of being void return

不同之处在于 Enumerable.Reverse() 返回一个 IEnumerable<T> 而不是 void return

回答by Des Horsley

I realize that this question is quite old, but I had a similar problem, except my string had spaces included in it. For those that need to know how to separate a string with more than just commas:

我意识到这个问题已经很老了,但我遇到了类似的问题,只是我的字符串中包含空格。对于那些需要知道如何用多个逗号分隔字符串的人:

string str = "Tom, Scott, Bob";
  IList<string> names = str.Split(new string[] {","," "},
  StringSplitOptions.RemoveEmptyEntries);

The StringSplitOptions removes the records that would only be a space char...

StringSplitOptions 删除只会是空格字符的记录...

回答by ablaze

If you are trying to

如果你想

  1. Use multiple delimiters
  2. Filter any empty strings
  3. Trim leading/trailing spaces
  1. 使用多个分隔符
  2. 过滤任何空字符串
  3. 修剪前导/尾随空格

following should work:

以下应该工作:

string str = "Tom Cruise, Scott, ,Bob | at";
IEnumerable<string> names = str
                            .Split(new char[]{',', '|'})
                            .Where(x=>x!=null && x.Trim().Length > 0)
                            .Select(x=>x.Trim());

Output

输出

  • Tom
  • Cruise
  • Scott
  • Bob
  • at
  • 汤姆
  • 巡航
  • 斯科特
  • 鲍勃

Now you can obviously reverse the order as others suggested.

现在您显然可以按照其他人的建议颠倒顺序。