Return a list of strings by splitting a string using the delimiter - CSharp System

CSharp examples for System:String Split

Description

Return a list of strings by splitting a string using the delimiter

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w w w.  ja  v  a2s.co m*/

public class Main{
        /// <summary>
        /// Return a list of strings by splitting a string using the delimiter
        /// </summary>
        /// <param name="s"></param>
        /// <param name="delimiter"></param>
        /// <returns></returns>
        public static List<string> GetSeparateStrings(string s, char delimiter)
        {
            List<string> res = new List<string>();
            if (String.IsNullOrEmpty(s))
                return res;

            string[] parts = s.Split(delimiter);
            foreach (string p in parts)
            {
                if (!String.IsNullOrEmpty(p))
                    res.Add(p);
            }
            return res;
        }
}

Related Tutorials