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
split(",")cuts a CSV line into a string array.String.joinglues any pieces back with a separator.- The argument is a regex:
\\s+splits on runs of whitespace.
Keywords and builtins used here
var
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.
Step 3 of 4 in Strings, step 6 of 29 in Language basics.