When you’re starting out with programming, understanding string manipulation is crucial. Strings are one of the most used data types in any application—from displaying user messages to handling input or connecting with databases.

In this guide, you’ll learn what strings are, how they work, and how to perform common string manipulation tasks using built-in methods. Whether you’re trimming whitespace or replacing characters, mastering these operations will give you a solid foundation.
Table of Contents
What Is a String?
A string is a sequence of characters, such as letters, numbers, or symbols. In C#, strings are enclosed in double quotes:
string greeting = "Hello, world!";
Strings in C# are immutable, meaning once they are created, their content cannot be changed. Any operation that modifies a string actually returns a new one.
Common String Methods for Beginners
Let’s explore some essential string methods that help with everyday string manipulation.
Length
Get the number of characters in a string:
string name = "Alice";
Console.WriteLine(name.Length); // Output: 5
ToUpper() and ToLower()
Convert strings to uppercase or lowercase:
string word = "Hello";
Console.WriteLine(word.ToUpper()); // HELLO
Console.WriteLine(word.ToLower()); // hello
Trim(), TrimStart(), TrimEnd()
Removes whitespace from the start and/or end.
string input = " padded ";
Console.WriteLine(input.Trim()); // "padded"
Substring()
Extract part of a string:
string message = "Welcome";
Console.WriteLine(message.Substring(0, 4)); // "Welc"
Replace()
Replaces one character or substring with another:
string text = "apples and oranges";
Console.WriteLine(text.Replace("apples", "bananas"));
Contains()
Check if a string contains a specific value:
string phrase = "I love programming";
Console.WriteLine(phrase.Contains("love")); // true
Split()
Splits a string into an array of substrings.
string colors = "red,blue,green";
string[] list = colors.Split(',');
IndexOf() and LastIndexOf()
Returns the index of the first or last occurrence of a character.
int first = text.IndexOf("l"); // 2
int last = text.LastIndexOf("l"); // 10
StartsWith() and EndsWith()
Checks the beginning or end of a string.
bool starts = text.StartsWith("Hello"); // true
bool ends = text.EndsWith("!"); // true
String Interpolation
Instead of concatenation, string interpolation offers a cleaner way to combine values into strings:
string name = "Alice";
int age = 30;
Console.WriteLine($"My name is {name} and I am {age} years old.");
Escaping Characters
To include quotes or special characters, escape them:
string quote = "She said, \"Hello!\"";
Or use verbatim strings:
string filePath = @"C:\\Users\\Public";
Working with Null or Empty Strings
string empty = "";
string nullString = null;
Console.WriteLine(string.IsNullOrEmpty(empty)); // true
Console.WriteLine(string.IsNullOrEmpty(nullString)); // true
Common Mistakes Beginners Make
- Not checking for null or empty strings
Use String.IsNullOrEmpty() or String.IsNullOrWhiteSpace() to validate. - Misunderstanding immutability
Every method like Replace() returns a new string; the original stays the same. - Overusing
+for concatenation
Use StringBuilder for better performance in loops.
Best Practices for String Manipulation
- Avoid unnecessary string concatenation in loops.
- Use StringBuilder for performance when working with many modifications.
- Use string.IsNullOrWhiteSpace() for validation.
Real-World Examples
Formatting Usernames
string username = " JohnDoe ";
username = username.Trim().ToLower();
Masking Emails
string email = "user@example.com";
int atIndex = email.IndexOf("@");
string masked = email.Substring(0, 2) + "***" + email.Substring(atIndex);
Learn More
Leave a Reply