Split string

Split method splits a string by specified characters and returns an array of strings.

By default Split method uses space as the separator.


using System;

class Sample
{
    public static void Main()
    {
        string s = "this is a test from java2s.com";
        string[] arr = s.Split();
        foreach (string ss in arr)
        {
            Console.WriteLine(ss);
        }
    }
}

The output:


this
is
a
test
from
java2s.com

To split with more than one delimiters use the overloaded version:


using System;

class Sample
{
    public static void Main()
    {
        string s = "this is a test from java2s.com";
        string[] arr = s.Split(new char[] { ' ', 't' });
        foreach (string ss in arr)
        {
            Console.WriteLine(ss);
        }
    }
}

The output:


his
is
a

es

from
java2s.com

Split also accepts a StringSplitOptions enum.

We can use StringSplitOptions to remove empty entries.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.