Search⌘ K
AI Features

Basic String Operations

Explore essential string operations in C# such as slicing, concatenation, splitting, and joining. Understand each operation's behavior, how immutability affects them, and analyze their time and space complexity for efficient string manipulation and algorithm implementation.

We'll cover the following...

Now that we’ve established that strings are immutable and that any apparent modification produces a new string rather than altering the original, the next step is to build familiarity with the operations that are actually available on strings. These operations form the practical foundation of string algorithm implementation. Each one has a defined behavior and a measurable cost, and understanding both is essential before moving on to writing algorithms that use them.

Slicing: Extracting part of a string

Slicing is the operation of extracting a contiguous portion of a string. The extracted portion is called a substring, and it is returned as a new string. The original string remains unchanged.

In C#, range expressions can be used to slice a string with the following syntax:

s[start..end]

The result contains all characters from the start index up to, but not including, the end index. Consider the following example:

C# 14.0
using System;
// Initialization of the string data
string s = "hello world";
// Accessing the first five characters (indices 0 to 4)
// The character at index 5 (' ') is excluded.
Console.WriteLine($"Substring from index 0 to 5: \"{s[0..5]}\"\n");
// Accessing characters from index 6 to the end (indices 6 to 10)
// This represents the second word in the sequence.
Console.WriteLine($"Substring from index 6 to 11: \"{s[6..11]}\"\n");
// Accessing a shorter prefix (indices 0 to 2)
Console.WriteLine($"Substring from index 0 to 3: \"{s[0..3]}\"");

In this example, s[0..5] extracts the first five characters, and s[6..11] extracts the last five. The character at the end index is never included in the result.

Now, let’s look at the following visualizer for a better understanding of slicing in strings:

Omitting start and end in slicing

If the start or end value is omitted, C# range expressions use default values:

  • When the start index is omitted, the slice begins at the first character.

  • When the end index is omitted, the slice extends to the last character.

  • When both are omitted, the entire string is returned as a new copy.

C# 14.0
using System;
string s = "hello world";
// Accessing from the start of the string to index 4 (indices 0 to 4)
// When the start index is omitted, it defaults to 0.
Console.WriteLine($"Substring from the start to index 5: \"{s[..5]}\"\n");
// Accessing from index 6 to the end of the string
// When the end index is omitted, it defaults to the length of the string.
Console.WriteLine($"Substring from index 6 to the end: \"{s[6..]}\"\n");
// Accessing the entire string sequence
// Omitting both indices creates a complete copy of the string.
Console.WriteLine($"Full string copy using slicing: \"{s[..]}\"");

Selecting characters with a step

C# range expressions do not include a built-in step value. To select every second character, or to traverse a string in reverse, we use a loop and build a new string.

var result = new StringBuilder();
for (int i = start; i < end; i += step)
{
result.Append(s[i]);
}

Incrementing the index by two selects every second character, and moving the index backward traverses the string in reverse order.

C# 14.0
using System;
using System.Text;
string s = "hello world";
// A loop can select characters using a fixed step between indices.
// Indices accessed: 0, 2, 4, 6, 8, 10
var everySecond = new StringBuilder();
for (int i = 0; i < s.Length; i += 2)
{
everySecond.Append(s[i]);
}
Console.WriteLine($"Every second character from the string: \"{everySecond}\"\n");
// Accessing the string in reverse order
// Start from the end and move toward the beginning.
var reversed = new StringBuilder();
for (int i = s.Length - 1; i >= 0; i--)
{
reversed.Append(s[i]);
}
Console.WriteLine($"The string in reverse order: \"{reversed}\"");

Complexity analysis of slicing

Producing a slice of length kk requires allocating a new string and copying ...