Extracting substrings

Extracting substrings

How to:

C# makes pulling substrings out of a string easy. Here’s a quick look at how it’s done using the Substring method and string slicing with range operators.

string fullString = "Hello, World! Life is beautiful.";
// Using Substring(startIndex, length)
string extracted1 = fullString.Substring(7, 5); // "World"

Console.WriteLine(extracted1); // Output: World

// Using string slicing with range operator [..]
string extracted2 = fullString[13..24]; // "Life is beau"

Console.WriteLine(extracted2); // Output: Life is beau

Deep Dive

Substrings aren’t a new trick. They’ve been in languages like C and Java for ages. However, C# has refined the process with methods and features that prioritize readability and ease of use.

Historically, programmers used loops and careful index calculations. The Substring method in C# is a sweet upgrade. It’s straightforward—give it a start index and, optionally, a length, and it does the slicing for you.

The spectacle doesn’t end there. With C# 8.0 and onwards, we’ve been introduced to range operators like [..]. They allow for more natural slicing expressions, especially when using indexes relative to the end of the string (denoted by the ^ operator).

Alternatives to Substring include methods like Split, Regex operations, or string manipulation with LINQ. The choice depends on the situation—you might split a CSV line, Regex a pattern, or pluck substrates with a fancy LINQ expression.

On the implementation side, C# strings are immutable. When you take a substring, you’re not altering the original. Instead, you’re minting a fresh string that shares some of the parent’s memory space — until you alter it, and then it’s off to its own memory allocation.

See Also

If you’re up for diving deeper or exploring related topics, here are some resources: