Chapter 3 · Data

Working with text

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A String is a row of characters

In this lesson you take text apart, compare it and format it.

String word = "Browser"; puts an arrow in the box word. The text sits on the heap, one character after another. Each character has a number, its index, counted from 0.

callgives
word.length()7: how many characters
word.charAt(0)B: the character at index 0
word.charAt(6)r: the last one, at length() - 1
public class Parts {
    public static void main(String[] args) {
        String word = "Browser";
        int last = word.length() - 1;
        System.out.println("length: " + word.length());
        System.out.print("first and last: " + word.charAt(0));
        System.out.println(word.charAt(last));
        System.out.print("from 0 to 3: ");
        System.out.println(word.substring(0, 3));
        System.out.println("from 3 on: " + word.substring(3));
        System.out.println("index of s: " + word.indexOf("s"));
        System.out.println("index of z: " + word.indexOf("z"));
        System.out.print(word.toUpperCase() + " ");
        System.out.println(word.toLowerCase());
        System.out.println("[" + "   Ada  ".trim() + "]");
    }
}
public class Email {
    static String user(String email) {
        // before the @, trimmed, in lower case
        return "";
    }

    static String domain(String email) {
        // after the @, trimmed, in lower case
        return "";
    }

    public static void main(String[] args) {
        String email = "  [email protected] ";
        String other = "[email protected]";
        System.out.println("User: " + user(email));
        System.out.println("Domain: " + domain(email));
        System.out.println("User: " + user(other));
        System.out.println("Domain: " + domain(other));
    }
}
public class Upper {
    public static void main(String[] args) {
        String name = "ada";
        name.toUpperCase();
        System.out.println(name);
    }
}
public class Same {
    public static void main(String[] args) {
        String a = "yes";
        String b = "YES".toLowerCase();
        System.out.println(a == b);
        System.out.println(a.equals(b));
    }
}
public class Capitals {
    public static void main(String[] args) {
        String text = "Java Runs In The Browser";
        int capitals = 0;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                capitals++;
            }
        }
        double share = 100.0 * capitals / text.length();
        System.out.printf("%s: %d capitals in %d characters%n",
                text, capitals, text.length());
        System.out.printf("%.2f percent capitals%n", share);
        System.out.printf("%-8s|%5d|%n", "upper", capitals);
        System.out.printf("%-8s|%5d|%n", "all", text.length());
    }
}
public class Digits {
    static int countDigits(String text) {
        // how many characters of text are '0' to '9'
        return 0;
    }

    static void report(String text) {
        // one line, with printf
    }

    public static void main(String[] args) {
        report("R2-D2");
        report("2026-10-16");
        report("hello");
    }
}
public class Names {
    // true when a and b are the same name, whatever
    // the spaces around them and their case
    static boolean same(String a, String b) {
        return false;
    }

    // the first letter of each of the two words,
    // in upper case
    static String initials(String fullName) {
        return "";
    }

    // the line for one name: see the expected output;
    // the number at the end is vowels / length
    static String card(String fullName) {
        return "";
    }

    public static void main(String[] args) {
        System.out.println(same("ADA", " ada"));
        System.out.println(same("Ada", "Alan"));
        System.out.println(initials("grace hopper"));
        System.out.println(card("  Grace HOPPER "));
        System.out.println(card("Alan Turing"));
    }
}
public class Palindrome {
    static String reversed(String text) {
        // the characters of text, last to first
        return text;
    }

    static void test(String word) {
        String low = word.toLowerCase();
        boolean same = low.equals(reversed(low));
        System.out.println(word + ": " + same);
    }

    public static void main(String[] args) {
        test("level");
        test("Java");
        test("Racecar");
    }
}
Terminal
Step 1 of 8Inside a String

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)