Split String In C#

Split String In C#

Split String in C#

   There are  many situations where we need to Split the string either at spaces or comma.
This post describes most common use of Split function. Here you can see how to remove empty strings
using
StringSplitOptions

Split string at comma: 

Code:
  

protected void Button1_Click(object sender, EventArgs e)
    {

        String str = "Now, Split string at comma";

        string []arr = str.Split(",".ToCharArray());


        foreach(string s in arr)
        {
            Response.Write( s  + "</br>");
        }


    }

Output :

 

 Now
 Split string at comma


Description: Here input string contains one comma and array arr holds two strings after splitteing str at comma.
The foreach loop iterates array element and print each element line by line.

Split string at multiple characters :

protected void Button1_Click(object sender, EventArgs e)
    {

        String str = "Now, Split string at comma. dot and - dash ";

        string []arr = str.Split(",.-".ToCharArray());


        foreach(string s in arr)
        {
            Response.Write( s  + "</br>");
        }


    }



Output:

 Now
 Split string at comma
 dot and
 dash

Description: The input string has been split at every occurances of comma, dot and dash.

String split Options:

Without using Split string options.

  protected void Button1_Click(object sender, EventArgs e)
    {

        String str = "  Split  string  Options ";

        string []arr = str.Split(" ".ToCharArray());


        foreach(string s in arr)
        {
            Response.Write( s  + "</br>");
        }


    }

OutPut:

 Split

 string

 Options


Description: Without using SplitStringOptions we get 8 elements of string array with blank spaces. 
                 To remove white spaces we need to use SplitStringOptions.

SplitstringOptions is an enum with none and RemoveEmptyEntries as value.

Code:

protected void Button1_Click(object sender, EventArgs e)
    {

        String str = "  Split  string  Options ";

        string []arr = str.Split(" ".ToCharArray(),

StringSplitOptions.RemoveEmptyEntries );


        foreach(string s in arr)
        {
            Response.Write( s  + "</br>");
        }


    }


OutPut:
 Split
 string
 Options


Description: The overload split with option Remove empty entries removes empty strings.

Tags:

C# DateTime ToString()Replace string in C#[C#] Remove Last Character From StringC# foreach loopc# stringHow To Declare and access array of string in C#.net String vs stringAsp.net Convert String To ImageC# Binary To StringC# Convert String To DateTimeConvert Array To Comma Separated String C#

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.