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

后端 未结 6 577
Happy的楠姐
Happy的楠姐 2020-12-07 19:49

What is the correct syntax for this:

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

What am I

相关标签:
6条回答
  • 2020-12-07 20:11

    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...

    0 讨论(0)
  • 2020-12-07 20:19

    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

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

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

    0 讨论(0)
  • 2020-12-07 20:19

    If you are trying to

    1. Use multiple delimiters
    2. Filter any empty strings
    3. Trim leading/trailing spaces

    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.

    0 讨论(0)
  • 2020-12-07 20:22
    List<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList();
    

    This one works.

    0 讨论(0)
  • 2020-12-07 20:31

    The problem is that you're calling List<T>.Reverse() which returns 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.)

    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.

    0 讨论(0)
  • 2020-12-07 20:32

    Try this:

    List<string> names = new List<string>("Tom,Scott,Bob".Split(','));
    names.Reverse();
    
    0 讨论(0)
提交回复
热议问题