Create a List<string> from a Delimited string

Here’s how to create a List<string> from a Delimited string using the Enumerable.ToList method

C#

static void Main(string[] args)
{
IEnumerable<string> lstNew = null;
string s = "jQuery MooTools Prototype";
char separator = ' ';
lstNew = s.Split(separator).ToList();
PrintList(lstNew);
Console.ReadLine();
}

static void PrintList(IEnumerable<string> str)
{
foreach (var s in str)
Console.WriteLine(s);
Console.WriteLine("-------------");
}

VB.NET

Sub Main()
Dim lstNew As IEnumerable(Of String) = Nothing
Dim
s As String = "jQuery MooTools Prototype"
Dim separator As Char = " "c
lstNew = s.Split(separator).ToList()
PrintList(lstNew)
Console.ReadLine()
End Sub

Shared Sub
PrintList(ByVal str As IEnumerable(Of String))
For Each s In str
Console.WriteLine(s)
Next s
Console.WriteLine("-------------")
End Sub

image

Read more tips in my article over here Some Common Operations using List<string>






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: