ZetCode

C# split string

last modified February 17, 2021

C# split string tutorial shows how to split strings in C# language. C# tutorial is a comprehensive tutorial on C# language.

In C#, a string is a sequence of Unicode characters.

There are several ways how to add strings in C#:

C# add strings with + operator

The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded.

Program.cs
using System;

var a = "an old";
var b = " falcon";

var c = a + b;
Console.WriteLine(a + b);

The example adds two strings using the + operator.

$ dotnet run
an old falcon

In the second example, we use the compound addition operator.

Program.cs
using System;

var msg = "There are ";

msg += "three falcons ";
msg += "in the sky";

Console.WriteLine(msg);

The example builds a message with the += operator.

$ dotnet run
There are three falcons in the sky

C# add strings with string.Concat

The string.Concat method concatenates one or more instances of string.

Program.cs
using System;

var a = "and old";
var b = " eagle";

var c = string.Concat(a, b);

Console.WriteLine(c);

The example concatenates two strings with the string.Concat method.

C# add strings with string.Join

The string.Join method concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.

Program.cs
using System;
using System.Collections.Generic;

var words = new List<string>{"There", "are", "two", "owls", "on", "the", "tree"};
var msg = string.Join(" ", words);

Console.WriteLine(msg);

In the example, we concatenate strings of a list.

$ dotnet run
There are two owls on the tree

C# add strings with StringBuilder

StringBuilder is a mutable sequence of characters. Its Append method appends the specified string to the string instance.

Program.cs
using System;
using System.Text;

var builder = new StringBuilder("There");

builder.Append(" are");
builder.Append(" two");
builder.Append(" falcons");
builder.Append(" in");
builder.Append(" the");
builder.Append(" sky");

Console.WriteLine(builder);

In the example, we form a new string with StringBuilder.

$ dotnet run 
There are two falcons in the sky

C# add strings with string interpolation

We can build C# strings with string interpolation. Interpolated strings start with the $ character.

Program.cs
using System;

var w1 = "three";
var w2 = "owls";

var msg = $"There are {w1} {w2} on the tree";

Console.WriteLine(msg);

Inside interpolated strings, the variables inside the curly brackets {} are expanded.

$ dotnet run
There are three owls on the tree

C# add strings with string.Format

The string.Format converts the value of objects to strings based on the formats specified and inserts them into newly formed string.

Program.cs
using System;

var w1 = "three";
var w2 = "owls";

var msg = string.Format("There are {0} {1} on the tree", w1, w2);

Console.WriteLine(msg);

We build a new string with string.Format

In this tutorial we have showed several ways how to add strings in C#.

Read C# tutorial or list all C# tutorials.