typestar

String methods in Java

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

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

System.out.println(text.toUpperCase());
System.out.println(text.replace("quick", "sly"));
System.out.println(text.contains("fox"));
System.out.println(text.startsWith("The"));
System.out.println(text.substring(4, 9));
System.out.println(text.length());

How it works

  1. trim strips the whitespace both ends carried in.
  2. toUpperCase, replace, contains and startsWith do daily work.
  3. substring(4, 9) slices by start and end; length counts chars.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
313
Tokens
110
Three-star pace
105 tpm

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

Type this snippet

Step 1 of 4 in Strings, step 4 of 29 in Language basics.

← Previous Next →

String methods in other languages