typestar

Split & join in C#

Strings to collections and back again.

var csv = "alpha,beta,gamma,delta";
string[] parts = csv.Split(',');
Console.WriteLine($"{parts.Length} parts, first {parts[0]}");

var joined = string.Join(" | ", parts);
Console.WriteLine(joined);

// split on runs of spaces, dropping the empties between them
var words = "one  two   three".Split(' ',
    StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(words.Length);

How it works

  1. Split(',') cuts a CSV line into a string array.
  2. string.Join glues any sequence back together with a separator.
  3. RemoveEmptyEntries drops the blanks that repeated spaces create.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
376
Tokens
67
Three-star pace
95 tpm

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

Type this snippet

Step 3 of 4 in Strings, step 6 of 29 in Language basics.

← Previous Next →

Split & join in other languages