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
string.Split(char separator) string.Split(string separator) string.Split(char[] separators) string.Join(string separator, IEnumerable<string> values) string.Join(string separator, string[] values)
Method List
| Method | Description |
|---|---|
| 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
Program.cs
using System;
// Use Split() to split a comma-separated string
string csvRow = "item_a,100,type_x,category_1,option_2";
string[] fields = csvRow.Split(',');
foreach (string field in fields) {
Console.WriteLine(field);
}
This produces the following output:
dotnet run item_a 100 type_x category_1 option_2
Joining with string.Join()
Use string.Join() to concatenate array or list elements with a delimiter.
JoinSample.cs
using System;
// Join an array using string.Join()
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
// Using a string as the delimiter
string sentence = "item_a, item_b, item_c";
string[] members = sentence.Split(", ");
Console.WriteLine(members[0]); // item_a
Console.WriteLine(members.Length); // 3
This produces the following output:
dotnet run C# / Java / Python csharp | dotnet | programming item_a 3
Multiple Delimiters / Removing Empty Entries
Passing a char[] lets you split on any of multiple delimiter characters. Use StringSplitOptions.RemoveEmptyEntries to exclude empty elements.
MultiSeparator.cs
using System;
// Split on multiple delimiter characters
string text = "apple;banana,cherry|grape";
string[] items = text.Split(new char[] { ';', ',', '|' });
Console.WriteLine(string.Join(", ", items)); // apple, banana, cherry, grape
// When delimiters appear consecutively
string csv = "a,,b,,c";
string[] withEmpty = csv.Split(',');
Console.WriteLine(withEmpty.Length); // 5 (includes empty entries)
string[] noEmpty = csv.Split(',', StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(noEmpty.Length); // 3 (empty entries excluded)
Console.WriteLine(string.Join(", ", noEmpty)); // a, b, c
This produces the following output:
dotnet run apple, banana, cherry, grape 5 3 a, b, c
Common Mistakes
Common Mistake: Empty Entries When Delimiters Are Consecutive
When delimiters appear consecutively or at the start/end of a string, Split() includes empty elements in the result array. Use StringSplitOptions.RemoveEmptyEntries to avoid unintended empty entries.
using System;
// NG: Consecutive delimiters produce empty entries
string csv = ",apple,,banana,";
string[] result = csv.Split(',');
Console.WriteLine(result.Length); // 5 (includes 3 empty entries)
foreach (string s in result) {
Console.WriteLine($"[{s}]");
}
This produces the following output:
dotnet run 5 [] [apple] [] [banana] []
Here is the source file used above:
using System;
// OK: Use RemoveEmptyEntries to exclude empty elements
string csv = ",apple,,banana,";
string[] result = csv.Split(',', StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(result.Length); // 2
foreach (string s in result) {
Console.WriteLine($"[{s}]");
}
This produces the following output:
dotnet run 2 [apple] [banana]
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 contact us.