typestar

String methods in C#

The everyday string toolkit: trim, transform, search and slice.

var raw = "  The quick brown fox  ";
var text = raw.Trim();

Console.WriteLine(text.ToUpper());
Console.WriteLine(text.Replace("quick", "sly"));
Console.WriteLine(text.Contains("fox"));
Console.WriteLine(text.StartsWith("The"));
Console.WriteLine(text.Substring(4, 5));
Console.WriteLine(text.Length);

How it works

  1. Trim strips the whitespace both ends carried in.
  2. ToUpper, Replace, Contains and StartsWith cover most daily work.
  3. Substring(4, 5) slices by start and length; Length counts characters.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
301
Tokens
86
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 49 seconds.

Type this snippet

Step 2 of 4 in Strings, step 5 of 29 in Language basics.

← Previous Next →

String methods in other languages