typestar

Split & join in Java

Strings to arrays and back again.

var csv = "alpha,beta,gamma,delta";
String[] parts = csv.split(",");
System.out.println(parts.length + " parts, first " + parts[0]);

var joined = String.join(" | ", parts);
System.out.println(joined);

// split takes a regex: any run of whitespace
var words = "one  two   three".split("\\s+");
System.out.println(words.length);

How it works

  1. split(",") cuts a CSV line into a string array.
  2. String.join glues any pieces back with a separator.
  3. The argument is a regex: \\s+ splits on runs of whitespace.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
328
Tokens
91
Three-star pace
95 tpm

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

Type this snippet

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

← Previous Next →

Split & join in other languages