C# Split Delimiter Use

Split strings

You are interested in details on how the Split method in the C# programming language can specify its delimiters, and also how you can control the output array length with the count parameter. To illustrate these cases, we test the Split method with more parameters.

Example

This program text is meant to demonstrate some aspects of the Split method on the string type. It shows how you can use the count parameter on the Split method, and also demonstrates the different ways of specifying delimiters in the method invocation. The GetValues1 method contains the simplest call, while the GetValues2 and GetValues3 methods show delimiter arrays.

This C# example program shows how to use delimiters on the Split method.

Program that uses Split with delimiters and count [C#]

using System;

class Program
{
    static void Main()
    {
	// Declare the input string containing separated values.
	// ... The three methods split with different code.
	const string input = "A;B;C,D,E,F;G;H;I;J";
	var array1 = GetValues1(input);
	var array2 = GetValues2(input);
	var array3 = GetValues3(input);

	// Display the three results.
	foreach (string value in array1)
	{
	    Console.Write(value + "  ");
	}
	Console.WriteLine();
	foreach (string value in array2)
	{
	    Console.Write(value + "  ");
	}
	Console.WriteLine();
	foreach (string value in array3)
	{
	    Console.Write(value + "  ");
	}
	Console.WriteLine();
    }

    const int _max = 5; // Maximum number of results

    static string[] GetValues1(string input)
    {
	// This just does a Split on the semicolon character.
	// ... It is for comparison to the other methods.
	return input.Split(';');
    }

    static string[] GetValues2(string input)
    {
	// This creates its delimiter array each time.
	// ... It splits on two delimiters.
	return input.Split(new char[] { ';', ',' }, _max);
    }

    static char[] _delimiters = { ';', ',' };

    static string[] GetValues3(string input)
    {
	// This uses an external array that is already constructed.
	// ... It splits on two delimiters.
	return input.Split(_delimiters, _max);
    }
}

Output
    (Two spaces separate elements.)

A  B  C,D,E,F  G  H  I  J
A  B  C  D  E,F;G;H;I;J
A  B  C  D  E,F;G;H;I;J

Overview. This program demonstrates some aspects of the Split method in the .NET Framework. The Program class introduces four methods: the Main entry point, the GetValues1 method, the GetValues2 method, and the GetValues3 method. Each of the methods is passed an argument containing a string delimited with two punctuation character types. The Split methods parse this string in different ways.

Count parameter in Split method. In the methods GetValues2 and GetValues3, you can see that an integer value is passed as the second argument to the Split method. This is the count parameter, and it specifies how many elements you want in the resulting array. If you specify a value such as five, the resulting array will contain five elements even if it could split off more. All of the remaining data will be found in the final element in the result array. The second two result arrays in the program show this.

Programming tip

Array creation expressions. The second two Split example methods, GetValues2 and GetValues3, also show a difference in how you can specify the delimiter char[] array as a parameter. When GetValues2 is executed, each invocation will allocate a character array of length two on the managed heap. This is because an array is a reference type. However, GetValues3 is more efficient because it stores the array as a static field.

Split String Examples

Summary

The C# programming language

We looked at additional aspects of the Split method, mainly the ways of specifying delimiters and also the count parameter. We showed how the choice of parameters to the Split invocation can change the resulting array provided by the method. The Split method is one of the most useful methods in the base class library and it can greatly simplify cases where you must tokenize or parse strings.

String Type
.NET