Tuesday, July 23, 2013

CSharp C# example string split array to List object

CSharp C# example string split array to List object

This is a quick example of using the string  Split function directly to a System.Collections.Generic.List object and a quick Linq search

Key line:

List<string> listOfStuff = new List<string>(myCommaSeperatedListOfStuff.Split(new char[] { ',' }));


Output:

apple

press enter to finish


Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleStuff
{
    class Program
    {
        static void Main(string[] args)
        {

            string myCommaSeperatedListOfStuff = "one,two,apple,shoe";           
            List<string> listOfStuff = new List<string>(myCommaSeperatedListOfStuff.Split(new char[] { ',' }));
            Console.WriteLine(listOfStuff.First((findit) => findit == "apple"));
            Console.WriteLine();
            Console.WriteLine("press enter to finish");
            Console.ReadLine();

        }
    }
}



No comments:

Post a Comment