Search⌘ K
AI Features

Other Common String Operations

Explore key string operations in C# such as searching for substrings, comparing strings, converting case, and trimming whitespace. Understand how these functions support string algorithms and improve your ability to manipulate and analyze text data effectively.

We'll cover the following...

Knowing the basic string operations such as finding the length, traversal, indexing, concatenation, substring extraction, and splitting is an important start. Knowing additional string operations will make you better prepared for a wider range of string algorithms. A problem may ask whether two strings are equal, whether one string starts with another, whether a smaller string appears inside a larger one, or how to form a new string from existing ones.

Searching

Searching refers to the operation of locating a character or a substring within a larger string. At a fundamental level, this is accomplished by scanning through the string and examining each position until the target is found or the end of the string is reached.

Checking for existence

The simplest form of search determines whether a substring exists anywhere within the string. In C#, this is done using the Contains() method.

C# 14.0
using System;
string s = "hello world";
// Evaluating the presence of the substring "world" within the string s
// The Contains method performs a search to determine if the sequence exists.
// A boolean true is returned if the sequence is identified.
Console.WriteLine($"Is \"world\" present in the string? {s.Contains("world")}\n");
// Evaluating the presence of the substring "xyz" within the string s
// A boolean false is returned if the sequence is not identified.
Console.WriteLine($"Is \"xyz\" present in the string? {s.Contains("xyz")}");

The Contains() method returns true if the substring is found and false otherwise.

Finding the position

When the exact position of the first occurrence is needed, C# provides the IndexOf() method, which returns the starting index of the substring. If the substring is not present, IndexOf() returns -1.

C# 14.0
using System;
string s = "hello world";
// Retrieving the starting index of the first occurrence of "world"
// The IndexOf method returns the lowest index where the substring begins.
// If the sequence is identified, an integer index (6) is returned.
Console.WriteLine($"The starting index of \"world\" in the string: {s.IndexOf("world")}\n");
// Attempting to retrieve the starting index of the sequence "xyz"
// If the substring is not present within the string, the method returns -1.
Console.WriteLine($"The starting index of \"xyz\" (not present) in the string: {s.IndexOf("xyz")}");

If a missing substring should be treated as an error, C# code commonly checks the result of IndexOf() and throws an exception when the result is -1.

C# 14.0
using System;
string s = "hello world";
// Retrieving the starting index of the first occurrence of "world"
// This helper returns the lowest index where the substring begins.
// If the sequence is identified, an integer index (6) is returned.
Console.WriteLine($"The starting index of \"world\" in the string: {RequiredIndexOf(s, "world")}\n");
// Attempting to retrieve the starting index of the sequence "xyz"
// If the substring is not present within the string, the helper throws an exception.
Console.WriteLine($"The starting index of \"xyz\" (not present) in the string: {RequiredIndexOf(s, "xyz")}");
int RequiredIndexOf(string text, string substring)
{
int index = text.IndexOf(substring);
if (index == -1)
{
throw new InvalidOperationException("Substring not found.");
}
return index;
}

The choice between using IndexOf() directly and wrapping it with an error check depends on whether the absence of the substring should be treated as an error or ...