String list + . Split()

Asked

Viewed 1,049 times

6

The command string.Split() separates a string in a array of the kind string[]. How to use the string.Split() to separate strings in a list List < string>?

4 answers

10

The method Split() returns an array of string, to turn into a list you would have to convert it, for this you can use the method .ToList();:

string texto = "1;2;3;4;5";
List<string> listaString = texto.Split(';').ToList();

8


By . NET has no way because there are no methods (or overloads of Split()) in which return what you want, the API was made to result in a array and just like that. Either you make a method of your own that does this, which is almost certain to be inefficient, or you should turn the result into a list after getting the array.

To my disappointment this is inefficient (but it’s more than you do at hand). The situation is ridiculous and I’ll try to find out more. You can create the list like this:

Or you can use the method .ToList() of the LINQ that will end up calling the constructor above and will have, in essence, the same efficiency and will give a little more comfortable syntax.

using System.Collections.Generic;
using System.Linq;
                    
public class Program {
    public static void Main() {
        var lista1 = "1,2,3,4,5,6".Split(',').ToList();
        var array = "1,2,3,4,5,6".Split(',');
        var lista2 = new List<string>(array);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Apparently the only way for you to solve this optimally would be:

  • Create your own string (This has serious implications and will not work well
  • Create your own list (may be a little better, but will still generate difficulties at other points of interaction with the application)
  • Create the list with reflection and place the array in the list manually. This is possible because List has a array internal, but has to make sure and know that the implementation of List change your code will no longer work until you make adaptation. This is unlikely, but it may happen.
  • Ask the . NET team to create a constructor that accepts a array without making the copy. I wouldn’t even try because there are good reasons to do it, but better reasons not to.
  • "By . NET there’s no way", what do you mean? I know it’s not scope, but taking :P

  • The API of Split() does not return a List, only array, then what he asked to separate into a list there is no way, there are the options that I said to get the list.

  • I think he meant that the Split method doesn’t have a overload that returns a List<T> by default.

  • I don’t quite understand the scope of . NET, you mean that Linq does not belong to . NET?

  • 1

    @Wictorchaves better now? Let’s see if anyone knows something, but I doubt there’s a better way. https://stackoverflow.com/questions/54353009/efficiency-of-list-creation-from-array

4

To use this feature you need to import the following package, since the ToList is a method of Linq

using System.Linq;

Just put the method ToList after the method Split, as in the example below:

listStrLineElements = line.Split(',').ToList();

Source

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.