Strings

View saved

Create strings

String literals use double quotes. Verbatim strings with @ reduce escaping.

string name = "Ada";
string path = @"C:\Courses\csharp";
Console.WriteLine(name);
Console.WriteLine(path);

Interpolate values

Prefixed $ strings insert expressions in braces.

int completed = 4;
Console.WriteLine($"{name} finished {completed} lessons.");

Inspect and transform

Strings are immutable; methods return new strings.

string raw = "  C Sharp  ";
Console.WriteLine(raw.Trim());
Console.WriteLine(raw.ToUpperInvariant());
Console.WriteLine(raw.Contains("Sharp"));

Useful APIs

  • Split and Join for lists of pieces
  • StartsWith / EndsWith
  • string.IsNullOrWhiteSpace for input checks
  • Prefer ordinal or invariant comparisons when culture should not matter

Comments

One comment per signed-in account. Comments are saved with this page’s URL.