Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. string.Split() / string.Join()

string.Split() / string.Join()

The Split() method splits a string by a delimiter and returns an array. The string.Join() method joins array elements with a delimiter and returns a string.

Syntax

// Splits the string by the specified delimiter and returns a string array.
string.Split(char separator)
string.Split(string separator)
string.Split(char[] separators)

// Joins the elements of a collection with the specified separator and returns a string.
string.Join(string separator, IEnumerable<string> values)
string.Join(string separator, string[] values)

Method List

MethodDescription
Split(char separator)Splits the string at the specified single character and returns a string[].
Split(string separator)Splits the string at the specified substring and returns a string[].
Split(char[] separators)Splits the string at any one of the specified delimiter characters.
string.Join(string separator, ...)Returns a string that concatenates the elements of an array or list with separator. This is a static method, not an instance method.

Sample Code

using System;

// Use Split() to split a comma-separated string.
string csvRow = "Alice,25,New York,Engineer";
string[] fields = csvRow.Split(',');
foreach (string field in fields)
{
    Console.WriteLine(field);
}
// Alice / 25 / New York / Engineer

// Example using a string as the delimiter.
string sentence = "apple, banana, cherry";
string[] fruits = sentence.Split(", ");
Console.WriteLine(fruits[0]); // apple
Console.WriteLine(fruits.Length); // 3

// Use string.Join() to join an array.
string[] languages = { "C#", "Java", "Python" };
string joined = string.Join(" / ", languages);
Console.WriteLine(joined); // C# / Java / Python

// A common pattern: Split, process, then Join.
string tagString = "csharp,dotnet,programming";
string[] tags = tagString.Split(',');
string result = string.Join(" | ", tags);
Console.WriteLine(result); // csharp | dotnet | programming

Notes

Split() is an instance method (called on a string instance), while string.Join() is a static method (called on the string class itself). Keep this distinction in mind.

If delimiters appear consecutively (e.g., "a,,b"), empty elements are included in the resulting array. To exclude empty entries, use Split(',', StringSplitOptions.RemoveEmptyEntries).

For string padding and formatting, see PadLeft() / PadRight().

If you find any errors or copyright issues, please .