Lesson 06 of 30
String Manipulation
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
String Basics
Strings in C# are immutable sequences of Unicode characters represented by the string type (an alias for System.String). Every operation that appears to modify a string actually creates a new one.
string s = "Hello, World!";
Console.WriteLine(s.Length); // 13
Console.WriteLine(s.ToUpper()); // HELLO, WORLD!
Console.WriteLine(s.ToLower()); // hello, world!
Console.WriteLine(s.Replace("World", "C#")); // Hello, C#!
Useful String Methods
string text = " Hello, C#! ";
Console.WriteLine(text.Trim()); // "Hello, C#!"
Console.WriteLine(text.TrimStart()); // "Hello, C#! "
Console.WriteLine(text.Contains("C#")); // True
Console.WriteLine(text.StartsWith(" H")); // True
Console.WriteLine(text.IndexOf("C#")); // 9
Console.WriteLine(text.Substring(2, 5)); // "Hello"
Split and Join
string csv = "apple,banana,cherry";
string[] fruits = csv.Split(',');
foreach (var f in fruits)
Console.WriteLine(f);
string joined = string.Join(" | ", fruits);
Console.WriteLine(joined); // apple | banana | cherry
Verbatim and Raw Strings
// Verbatim string — backslashes are literal
string path = @"C:\Users\Alice\Documents";
// Raw string literal (C# 11+) — no escaping needed
string json = """
{
"name": "Alice",
"age": 30
}
""";
StringBuilder for Performance
When building strings in a loop, use StringBuilder to avoid creating hundreds of intermediate string objects:
using System.Text;
var sb = new StringBuilder();
for (int i = 1; i <= 5; i++)
sb.AppendLine($"Line {i}");
Console.WriteLine(sb.ToString());