Skip to lesson

String methods: quick reference

A review of this module. Skip it if you feel confident; the next module does not need it.

A one-page sheet of every String method from this module, grouped by job. Each line shows a call and its result, so you can look one up in seconds while you solve problems. Bookmark this page.

Inspect and cut

Use these to look inside a String or take a piece of it. Each line shows a call and, after //, its result.

Java
"Java".length()                 // 4"Java".charAt(0)                // 'J'"Hello".substring(1)            // "ello""Hello".substring(1, 3)         // "el""".isEmpty()                    // true"  ".isEmpty()                  // false"  ".isBlank()                  // true"Java".toCharArray()            // {'J', 'a', 'v', 'a'}

Indexes start at 0, and substring(1, 3) stops before index 3. Reading past the end, like "Java".charAt(4), throws StringIndexOutOfBoundsException. Taught in Essential String methods (opens in a new tab) and substring (opens in a new tab).

Search

Use these to find text inside a String. They are case-sensitive: "cv.pdf".endsWith(".PDF") is false.

Java
"banana".indexOf("a")            // 1"banana".indexOf("a", 2)         // 3"banana".indexOf("z")            // -1"banana".lastIndexOf("a")        // 5"banana".contains("nan")         // true"+919876543210".startsWith("+91") // true"cv.pdf".endsWith(".pdf")        // true

-1 means "not found", so check for it before you use the index. Taught in Essential String methods (opens in a new tab).

Compare

Use these to compare text. Never compare text with ==: it checks whether both sides are the same object, not the same characters.

Java
"Ravi".equals("ravi")              // false"Ravi".equalsIgnoreCase("ravi")    // true"Anu".compareTo("Ravi")            // -17"Ravi".compareTo("Ravi")           // 0"anu".compareToIgnoreCase("ANU")   // 0

compareTo gives a negative number when the first String comes first in dictionary order, 0 when they are equal, and a positive number otherwise. Taught in equals vs == (opens in a new tab).

Change: case, spaces and text

These never change the original String; each one gives back a new String, so store the result.

Java
"Java".toUpperCase()               // "JAVA""Java".toLowerCase()               // "java""  Hi  ".trim()                    // "Hi""  Hi  ".strip()                   // "Hi""  Hi  ".stripLeading()            // "Hi  ""  Hi  ".stripTrailing()           // "  Hi""15-08-2024".replace('-', '/')     // "15/08/2024""Hi Sir".replace("Sir", "Ma'am")   // "Hi Ma'am""ab".repeat(3)                     // "ababab""Rohit".concat(" Kumar")          // "Rohit Kumar"

name.toUpperCase(); on its own line does nothing useful: write name = name.toUpperCase();. Taught in Changing text (opens in a new tab).

Split, join and convert

Use these to break text into parts, glue parts together, or turn values into text and back.

Java
"Ravi,Anu,Meena".split(",")       // {"Ravi", "Anu", "Meena"}String.join("-", "Ravi", "Anu")    // "Ravi-Anu"String.valueOf(85)                 // "85"Integer.parseInt("85")            // 85String.format("%.2f", 9.5)        // "9.50""A1b2".matches("[A-Za-z0-9]+")    // true"A1b2".replaceAll("[0-9]", "")    // "Ab"

Example · Many methods in one small program

SignUpCheck.javaJava
public class SignUpCheck {    public static void main(String[] args) {        String name = "  priya sharma ";        String email = "Priya.Sharma@Mail.com";        String phone = "+91 98765 43210";
        String clean = name.strip();        String first = clean.substring(0, clean.indexOf(' '));        String initial = first.substring(0, 1).toUpperCase();        System.out.println("Hello, " + initial + first.substring(1) + "!");
        String mail = email.toLowerCase();        System.out.println("Email ok: " + (mail.contains("@") && mail.endsWith(".com")));
        String digits = phone.replace(" ", "").substring(3);        System.out.println("Phone: " + digits + " (" + digits.length() + " digits)");
        String[] parts = clean.split(" ");        System.out.println("Username: " + String.join("_", parts) + String.valueOf(digits.length()));    }}
Output
Hello, Priya!
Email ok: true
Phone: 9876543210 (10 digits)
Username: priya_sharma10

What this code does

  • strip() removes the outer spaces; indexOf(' ') finds where the first name ends, and substring cuts it out.
  • toUpperCase() on the first letter plus substring(1) gives a capital first letter, the usual way to tidy a name.
  • replace(" ", "") removes every space, split breaks the name into words, and String.join glues them with _.

matches and replaceAll take a pattern (a regular expression), which comes later in the course. To build long text in a loop, use StringBuilder (opens in a new tab). Taught in Splitting, joining and parsing (opens in a new tab).

Next step

Check your understanding

7 quick questions