String split

In this chapter you will learn:

  1. How to split a string
  2. Use the Split() method to split strings
  3. Split with space
  4. Split with a list of dividers
  5. Split by char array
  6. Split a string delimited by another string and return 2 non-empty elements
  7. Split string with \\

Split string

using System;/*from  j ava  2  s  .co m*/

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:

Use the Split() method to split strings

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;/*  j  a  v  a 2 s .com*/

class MainClass
{

  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString9 = String.Join(".", myStrings);    
    myStrings = myString9.Split('.');
    foreach (string mySplitString in myStrings)
    {
      Console.WriteLine("mySplitString = " + mySplitString);
    }
  }

}

The code above generates the following result.

mySplitString = To//from   ja  v  a2 s.com
mySplitString = be
mySplitString = or
mySplitString = not
mySplitString = to
mySplitString = be

Split with space

using System;/*from ja va2 s  .  c  o  m*/
using System.Text;
class SplitStringApp
{
    static void Main(string[] args)
    {
        string s = "Once Upon A Time In America from java2s.com";
        char[] seps = new char[]{' '};
        foreach (string ss in s.Split(seps))
            Console.WriteLine(ss);
    }
}

Split with a list of dividers

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

using System;/*from  j a  v a 2s. c  om*/

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:

Split by char array

using System;// ja v  a 2 s .  c om

public class Main
{
  static void Main(string[] args)
  {
    string MyString = "The quick brown fox ran around! java2s.com";
    string[] MyStringSplit = new string[6];

    MyStringSplit = MyString.Split(new char[] { ' ' }, 6);
    Console.WriteLine(MyStringSplit[1] + MyStringSplit[3]);
  }
}

Split a string delimited by another string and return 2 non-empty elements

Split also accepts a StringSplitOptions enum. We can use StringSplitOptions to remove empty entries.

using System;//j  a  v a 2s.c o m
class Sample 
{
    public static void Main() 
    {
        string s2 = "[divider]" +
                "A[divider][divider]" +
                "B[divider][divider][divider]" +
                "C[divider][divider]";
        string[] stringSeparators = new string[] {"[divider]"};
        string[] result;
        
        result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
        Show(result);
    }
    public static void Show(string[] entries)
    {
        foreach (string entry in entries)
        {
            Console.WriteLine(entry);
        }
    }
}

Split string with \\

using System;/*j a  v  a 2 s . co m*/

class Class1
{
  [STAThread]
  static void Main(string[] args)
  {
        char delim = '\\';
        string filePath = "C:\\Windows\\Temp";
        string [] directories = null;

        directories = filePath.Split( delim );
            
        foreach (string directory in directories) 
        {
            Console.WriteLine("{0}", directory);
        }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Search with IndexOf
  2. String from the middle
  3. String by case
  4. Search a character from the last with LastIndexOf
  5. Index of any characters
  6. Search a character in a string
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String