C# String Operations: Most Common Methods and Examples
Strings in C# represent sequences of characters and are one of the most frequently used data types. Whether you're handling user input, displaying messages, or processing data, string operations are essential.
In C#, strings are immutable, meaning once created, they cannot be changed. Any modification creates a new string in memory.
1. Creating Strings
string name = "John";
string message = "Hello World";
You can also use the String class:
string text = String.Empty;
2. String Concatenation (Combining Strings)
Using + Operator:
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
Using Interpolation (Recommended):
string fullName = $"{firstName} {lastName}";
Using String.Concat():
string result = String.Concat("Hello", " ", "World");
3. String Length
string text = "Hello";
Console.WriteLine(text.Length); // Output: 5
4. Converting Case
string text = "hello world";
Console.WriteLine(text.ToUpper()); // HELLO WORLD
Console.WriteLine(text.ToLower()); // hello world
5. Searching in Strings
Contains():
string text = "C# Programming";
bool result = text.Contains("C#"); // true
StartsWith() / EndsWith():
text.StartsWith("C#"); // true
text.EndsWith("ing"); // true
IndexOf():
int index = text.IndexOf("Programming"); // returns position
6. Extracting Substrings
string text = "Hello World";
string part = text.Substring(0, 5); // Hello
7. Replacing Text
string text = "Hello World";
string newText = text.Replace("World", "C#");
8. Removing Whitespace
string text = " Hello ";
Console.WriteLine(text.Trim()); // both sides
Console.WriteLine(text.TrimStart()); // start only
Console.WriteLine(text.TrimEnd()); // end only
9. Splitting Strings
string sentence = "C# is easy to learn";
string[] words = sentence.Split(' ');
10. Comparing Strings
string a = "hello";
string b = "HELLO";
Console.WriteLine(a == b); // false
Console.WriteLine(a.Equals(b, StringComparison.OrdinalIgnoreCase)); // true
11. String Formatting
string name = "Alice";
int age = 25;
string message = $"Name: {name}, Age: {age}";
12. Common String Methods
| Method | Description |
|---|---|
| Length | Returns number of characters |
| ToUpper() | Converts string to uppercase |
| ToLower() | Converts string to lowercase |
| Contains() | Checks if string contains a value |
| Substring() | Extracts part of string |
| Replace() | Replaces part of string |
| Trim() | Removes whitespace |
| Split() | Splits string into array |
13. Advantages and Disadvantages
Advantages
• Easy to use
• Rich built-in methods
• Widely used in all applications
Disadvantages
• Immutable (can impact performance in heavy modifications)
• Memory overhead for repeated changes
14. Common Mistakes
• Forgetting strings are immutable
• Using == instead of proper comparison
• Not handling null or empty strings
• Excessive concatenation in loops
15. Best Practices
• Use string interpolation ($"") instead of +
• Use StringBuilder for heavy modifications
• Always validate user input
• Use case-insensitive comparisons when needed