Search⌘ K
AI Features

String Operations

Explore essential string operations in C# to compare, combine, search, and modify text efficiently. Learn techniques for culture-aware comparisons, string interpolation, trimming, splitting, and case changes to handle strings effectively in your .NET applications.

The System.String class provides dozens of built-in methods. Let's look at some of the most critical operations that we can perform on string objects.

Compare

We can compare the equality of strings or their alphabetical ordering. To check alphabetical sequence, we use the CompareTo() method.

C# 14.0
string person1 = "Anna";
string person2 = "Darcy";
// Checking for equality
bool areSame = person1 == person2;
Console.WriteLine($"Same: {areSame}.");
// Negative result: person1 is before person2 (in terms of alphabetical order)
// Zero: person1 and person2 have the same position
// Positive result: person1 is after person2
int comparisonResult = person1.CompareTo(person2);
// Comparison result is negative because A precedes D
Console.WriteLine(comparisonResult);
person2 = "Albert";
comparisonResult = person1.CompareTo(person2);
// Now, the result is positive because An comes after Al
Console.WriteLine(comparisonResult);
  • Lines 1–2: We define two separate string variables representing names.

  • Lines 5–6: We evaluate if both strings are identical and print the resulting boolean value.

  • Lines 12–15: We execute CompareTo() to check alphabetical ordering. This prints a negative number since “Anna” comes before “Darcy”.

  • Lines 17–21: We update the second name to “Albert” and compare again. This yields a positive integer because “Anna” comes after “Albert”.

Culture-aware comparisons

Using the == operator performs an exact, case-sensitive match. When we need to compare strings without case sensitivity, we should avoid converting both strings using .ToLower() or .ToUpper(). Creating new strings just for comparison wastes memory. Instead, we use the string.Equals() method combined with the StringComparison.OrdinalIgnoreCase enumeration.

C# 14.0
string userInput = "ADMIN";
string targetRole = "admin";
// Inefficient approach (creates new memory allocations)
bool inefficientMatch = userInput.ToLower() == targetRole.ToLower();
Console.WriteLine($"Matched inefficiently: {inefficientMatch}");
// Modern, memory-efficient approach
bool efficientMatch = string.Equals(userInput, targetRole, StringComparison.OrdinalIgnoreCase);
Console.WriteLine($"Matched securely: {efficientMatch}");
  • Lines 1–2: We define two strings with identical text but different casing.

  • Line 5: We show the inefficient approach. Calling ToLower() allocates two completely new string objects in memory just to perform a check.

  • Line 6: We print the result of the inefficient match.

  • Line 9: We use string.Equals with StringComparison.OrdinalIgnoreCase to perform a direct, allocation-free comparison.

  • Line 10: We print the result of our secure, ...

String concatenation
String concatenation

Here is how we can implement these different combining techniques.

C# 14.0
string firstName = "Albert";
string lastName = "Johnson";
// Three ways to combine strings
string fullName = firstName + " " + lastName;
string fullName1 = string.Concat(firstName, " ", lastName);
string fullName2 = $"{firstName} {lastName}"; // String interpolation
Console.WriteLine(fullName);
Console.WriteLine(fullName1);
Console.WriteLine(fullName2);
// We can also join an array of strings with a delimiter
string[] names = ["Robert", "Dan", "Luke", "Marcus"];
string allNames = string.Join(", ", names);
Console.WriteLine(allNames);
  • Lines 1–2: ...