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>
?
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>
?
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:
string
(This has serious implications and will not work wellList
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.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();
1
Just to give one more option with Extension: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
public class Program
{
static void Main(string[] args)
{
var lst = "0,1,2,3,4,5".SplitList(',');
}
}
public static class Extensions
{
public static List<string> SplitList(this string str, char split)
{
return str.Split(split).ToList();
}
}
Browser other questions tagged c# .net array string list
You are not signed in. Login or sign up in order to post.
"By . NET there’s no way", what do you mean? I know it’s not scope, but taking :P
– Wictor Chaves
The API of
Split()
does not return aList
, 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.– Maniero
I think he meant that the Split method doesn’t have a
overload
that returns a List<T> by default.– Pedro Paulo
I don’t quite understand the scope of . NET, you mean that Linq does not belong to . NET?
– Wictor Chaves
@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
– Maniero