HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C#VariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Dictionary
🔀 Condition & Loop
🧩 Function
C#›Strings

Strings

Length, casing, searching (Contains), and replacing (Replace).

Simple enough for a 5-year-old to understand

Length and letter case

Example code
1
using System;
2
var s = "HexLibrary";
3
Console.WriteLine(s.Length);
10 (character count)
Output / Input10 (character count)
4
Console.WriteLine(s.ToUpper());
HEXLIBRARY
Output / InputHEXLIBRARY
5
Console.WriteLine(s.ToLower());
hexlibrary
Output / Inputhexlibrary
Output / Input
💡 Hint: Length is with .Length (capital L).

About this topic

In C#, a string is a reference type and is IMMUTABLE: ToUpper, Replace, Trim — all return a new string, leaving the original string as is. If you don't assign the result, nothing changes, which is the most common string mistake.

One of C#'s most practical features is string interpolation: if you put a dollar sign in front of the text, you can write variables and expressions directly inside curly braces. It is much more readable than concatenation and is standard in modern C# code.

StringBuilder is used when accumulating text inside a loop. Since each concatenation creates a new object, thousands of iterations will cause a significant slowdown; StringBuilder grows the same buffer.

What you will learn

  • Using Length, Substring, and indexing
  • String interpolation
  • The ToUpper, Trim, and Replace methods
  • Searching with Contains, StartsWith, and IndexOf
  • Splitting and joining with Split and Join
  • Efficient concatenation with StringBuilder

When to use?

String interpolation is the default way to generate text to be displayed to the user. Formatting can also be done within the same structure: date and currency formats are added directly to the expression.

When comparing user input, using case-insensitive comparison along with Trim is almost mandatory; otherwise, a leading space will break the match.

Common mistakes

Wrong

string name = "  Ada  ";
name.Trim();
Console.WriteLine(name.Length);

Trim does not modify the original string, it returns a new trimmed string. Because you didn't assign the result, name still has spaces and the length turns out longer than you expected. All string methods behave this way.

Correct

string name = "  Ada  ";
name = name.Trim();
Console.WriteLine(name.Length);

Wrong

string result = "";
for (int i = 0; i < 10000; i++) {
    result += i;
}

Each iteration generates a new string object; in ten thousand iterations, ten thousand objects are created and the operation slows down noticeably. StringBuilder should be used for concatenation inside loops.

Correct

var sb = new System.Text.StringBuilder();
for (int i = 0; i < 10000; i++) {
    sb.Append(i);
}
string result = sb.ToString();

Wrong

string text = "C#";
Console.WriteLine(text[2]);

Indices start at zero; the valid indices of a 2-character string are 0 and 1. You will get an "IndexOutOfRangeException". For the last character, you need to subtract one from the length.

Correct

string text = "C#";
Console.WriteLine(text[text.Length - 1]);

Frequently asked questions

String interpolation or concatenation?

Interpolation — its readability is significantly higher and the compiler translates it into efficient code. Concatenation is only acceptable when there are two pieces; after three pieces, interpolation is always clearer.

How do I check if a string is empty?

With string.IsNullOrWhiteSpace. This method catches null, empty string, and text containing only whitespace together — this is exactly the behavior you want with user input. Length checking throws an error in the case of null.

When is StringBuilder necessary?

When you are accumulating text inside a loop. It is unnecessary for a few one-off concatenations. The difference becomes apparent when the number of concatenations exceeds the hundreds; at that point, StringBuilder is many times faster.

Summary

In C# strings are immutable; every method returns a new string and you need to assign the result. String interpolation is the standard way to generate text, and StringBuilder is used inside loops.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code