← All lessons
Java basics · Lesson 4 of 10

Working with text

A String is a piece of text. Strings come with built-in methods, actions you call with a dot after the string: name.length() asks a string how many characters it has. The brackets are part of calling a method, even when there's nothing inside them.

Java
String word = "Java";
System.out.println(word.length());
System.out.println(word.toUpperCase());
System.out.println(word.toLowerCase());
System.out.println(word.charAt(0));
System.out.println(word.charAt(3));
Prints
4
JAVA
java
J
a

Each character has a position called its index, and counting starts at 0, not 1. In "Java", J is at 0, a at 1, v at 2 and the last a at 3. So the last character of any string is at length() - 1. Asking for an index that doesn't exist, like word.charAt(4), crashes the program with an error.

substring(start, end) gives a piece of the string from start up to, but not including, end. With one number it goes from there to the end.

Java
String s = "sunflower";
System.out.println(s.substring(0, 3));
System.out.println(s.substring(3));
System.out.println(s.indexOf("flow"));
System.out.println(s.contains("sun"));
System.out.println(s.replace("sun", "moon"));
Prints
sun
flower
3
true
moonflower

indexOf says where a piece first appears (or -1 if it isn't there), contains answers true or false, and replace gives a new string with every match swapped. A string itself never changes: methods like toUpperCase and replace give you a new string, so keep the result if you want it: s = s.toUpperCase();.

Comparing text

To check whether two strings hold the same text, use .equals(), never ==. The == sign asks whether they're the very same object in memory, which can be false even when the letters match. equalsIgnoreCase ignores capitals.

Java
String typed = new String("yes");
System.out.println(typed.equals("yes"));
System.out.println(typed == "yes");
System.out.println("YES".equalsIgnoreCase("yes"));
Prints
true
false
true

Joining and formatting

+ joins strings, and turns numbers into text when one side is a string. Java reads + left to right, which explains a classic surprise:

Java
System.out.println("Score: " + 2 + 3);
System.out.println("Score: " + (2 + 3));
System.out.println(2 + 3 + " points");
Prints
Score: 23
Score: 5
5 points

In the first line "Score: " + 2 is already text, so the 3 is joined on as text too. Brackets make the sum happen first. For anything longer, String.format (or printf, which prints straight away) is tidier: %s is replaced by a value, %d by a whole number and %.2f by a decimal with two places.

Java
String name = "Ana";
double total = 7.5;
System.out.println(String.format("%s owes $%.2f", name, total));
System.out.printf("%d items%n", 3);
Prints
Ana owes $7.50
3 items

Some characters need a backslash inside quotes: \" is a quote mark, \n starts a new line and \\ is one backslash. In printf, %n is the new line.

Java
System.out.println("She said \"hi\"");
System.out.println("line one\nline two");
Prints
She said "hi"
line one
line two

Practice

What does this print?

System.out.println("pizza".length());
More info

Quick reference

s.length()How many characters.
s.charAt(i)The character at index i; the first is at 0.
s.substring(a, b)From index a up to, not including, b.
s.indexOf(x) / s.contains(x)Where x first appears (-1 if not) / whether it's there.
s.toUpperCase() / replace(a, b)New strings; s itself doesn't change.
a.equals(b)Same text? Never use == for strings.
String.format("%s %d %.2f", ...)Fill in text, a whole number, a decimal with 2 places.
\" \n \\A quote, a new line, a backslash inside a string.

Related courses