DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

String split by multiple characters - C#.NET Example

By Ashok Nalam on 13 Dec 2013 | Category: C# | Tagged: string .NET 
Snippet shows how to split string by multiple characters delimiter using C#.NET Example.
  Discuss Add Comment   |  
Add rating Rate this resource   
Regularly we use string.Split function with single character delimiter. But if you want to split string by multiple characters delimiter use the following code.

Program:

using System;
using System.Text.RegularExpressions;
namespace DotNetMirror
{
    class StringSplitByMultiCharacter
    {
        static void Main(string[] args)
        {
            string str = "A,B,C,D,E";
            //split by single char
            string[] splitBySingleChar = str.Split(','); //output: A B C D E

            string strMultiChars = "A{,}B,{,}C{,}D,{,}E";
            //split by string array
            string[] splitByMultipleSring = strMultiChars.Split(new string[] { "{,}" }, StringSplitOptions.None);
            foreach (string s in splitByMultipleSring)
            {
                Console.WriteLine(s);
            }

            //alternative - split by regex
            string[] splitByMultipleCharRegEx = Regex.Split(strMultiChars, "{,}");
            Console.ReadKey();
        }
    }
}

Output:

A
B,
C
D,
E
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8926  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.