Basics Lesson 6 of 15
C# Strings and String Methods
Work with text in C# — Length, indexing, ToUpper, Trim, Substring, IndexOf, Replace, Split, Contains, comparison, verbatim strings and StringBuilder.
Strings hold text. In C#, string is a built-in type with many useful methods. Method names in C# start with a capital letter: ToUpper(), Trim(), Substring().
Common methods
string name = " Maria Clara ";
string clean = name.Trim();
Console.WriteLine(clean.Length);
Console.WriteLine(clean.ToUpper());
Console.WriteLine(clean.ToLower());
Console.WriteLine(clean[0]);
Console.WriteLine(clean.IndexOf("Clara"));
Console.WriteLine(clean.Substring(0, 5));
Console.WriteLine(clean.Substring(6));
Console.WriteLine(clean.Replace("a", "@"));
Console.WriteLine(clean.Contains("Clara"));
Console.WriteLine(clean.StartsWith("Mar"));
Output
11 MARIA CLARA maria clara M 6 Maria Clara M@ri@ Cl@r@ True True
| Member | Does |
|---|---|
Length | Number of characters (a property, no parentheses) |
s[i] | Character at index i (starts at 0) |
Substring(start, length) | Part of the string |
IndexOf(x) | Position, or -1 if not found |
Trim(), ToUpper(), ToLower() | Cleaned / changed copies |
Replace(a, b) | Replace all a with b |
Contains, StartsWith, EndsWith | true/false |
Like in Java and Python, strings are immutable: methods return new strings and never change the original.
Split and Join
string subjects = "Math,Science,English,Filipino";
string[] parts = subjects.Split(',');
Console.WriteLine(parts.Length);
foreach (string s in parts)
{
Console.WriteLine($"- {s}");
}
Console.WriteLine(string.Join(" | ", parts));
Output
4 - Math - Science - English - Filipino Math | Science | English | Filipino
Comparing strings
string answer = "YES";
Console.WriteLine(answer == "yes");
Console.WriteLine(answer.Equals("yes", StringComparison.OrdinalIgnoreCase));
Console.WriteLine(string.Compare("apple", "banana") < 0);
Console.WriteLine(string.IsNullOrWhiteSpace(" "));
Output
False True True True
string.IsNullOrWhiteSpace is perfect for checking if a user left an input empty.
Special string forms
string path = @"C:\Users\Juan\Documents"; // verbatim: backslashes are literal
string quote = "She said \"Kaya natin!\""; // escaped quotes
string menu = """
1. Pandesal
2. Kape
"""; // raw string literal (C# 11)
Console.WriteLine(path);
Console.WriteLine(quote);
Console.WriteLine(menu);
Output
C:\Users\Juan\Documents She said "Kaya natin!" 1. Pandesal 2. Kape
Looping through characters
string word = "Pilipinas";
int vowels = 0;
foreach (char c in word.ToLower())
{
if ("aeiou".Contains(c)) vowels++;
}
Console.WriteLine($"{word} has {vowels} vowels.");
Output
Pilipinas has 4 vowels.
StringBuilder
Joining strings many times in a loop creates many temporary strings. StringBuilder is much faster:
using System.Text;
var sb = new StringBuilder();
for (int i = 1; i <= 5; i++)
{
sb.Append(i);
if (i < 5) sb.Append(", ");
}
Console.WriteLine(sb.ToString());
Output
1, 2, 3, 4, 5
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04