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
Split(',')cuts a CSV line into a string array.string.Joinglues any sequence back together with a separator.RemoveEmptyEntriesdrops the blanks that repeated spaces create.
Keywords and builtins used here
stringvar
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.
Step 3 of 4 in Strings, step 6 of 29 in Language basics.