CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to split string using C#?
Submitted By Satheesh Babu B
On 11/10/2010 11:03:55 AM
Tags: C#,CodeDigest  

How to Split strings using C#?

Splitting strings based on a delimiter is one of the very common requirements we will get.  This little code snippet will help us to do with String.Split() method and RegEx.Split() method.


 
String.Split() method
  string str = "I love ASP.Net";
        string[] StrSpli = str.Split(' ');
 
        foreach (string s in StrSpli)
        {
            Response.Write(s);
            Response.Write("<BR>");
        }


 
RegEx.Split() method
string str = "I love ASP.Net";
        string[] StrSpli = Regex.Split(str, @"\s");
 
        foreach (string s in StrSpli)
        {
            Response.Write(s);
            Response.Write("<BR>");
        }


 
The above samples will split the string if there is a space between the words. You should include System.Text.RegularExpression namespace for the RegEx.Split() method to work.

 

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..