Split text with a separator char - CSharp System

CSharp examples for System:String Split

Description

Split text with a separator char

Demo Code


using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using System;/*from  ww  w. j  a  v  a 2s  .  com*/

public class Main{
        /// <summary>
        /// Split text with a separator char
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="sep">The separator.</param>
        /// <returns></returns>
        public static string[] SplitWithSeparator(string text, char sep)
        {
            if (String.IsNullOrEmpty(text)) return null;

            string[] items = new string[2];

            int index = text.IndexOf(sep);
            if (index >= 0)
            {
                items[0] = text.Substring(0, index);
                items[1] = text.Substring(index + 1);
            }
            else
            {
                items[0] = text;
                items[1] = null;
            }

            return items;
        }
}

Related Tutorials