Skip to lesson

Cut out part of a String with substring()

substring() copies a part of a String into a new String. You give the index where the part starts, and if you like, the index where it stops.

substring(start, end): the end is not included

A roll number like 2023CSE117 hides the year, the branch and the number. substring(start, end) copies the characters from start up to, but not including, end.

The roll number 2023CSE117 with indexes 0 to 9. substring(0, 4) gives 2023, substring(4, 7) gives CSE and substring(7) gives 117. The end index of one cut is not included, so it is the start of the next cut.

Example 1 · Parts of a roll number

RollParts.javaJava
public class RollParts {    public static void main(String[] args) {        String roll = "2023CSE117";        String year = roll.substring(0, 4);        String branch = roll.substring(4, 7);        String number = roll.substring(7);        System.out.println("Year: " + year);        System.out.println("Branch: " + branch);        System.out.println("Number: " + number);    }}
Output
Year: 2023
Branch: CSE
Number: 117

What this code does

  • substring(0, 4) copies index 0 to 3. Index 4 is not included, so it can start the branch.
  • substring(7) has no end, so it takes everything from index 7 to the end.
  • roll itself does not change: each call makes a new String.

Because the end is left out, the result always has end - start characters: substring(4, 7) has 3. And one cut ends exactly where the next one starts.

substring(start): everything after

A bank app shows only the last 4 digits of your phone number. With one number, substring(start) takes everything from start to the end.

Example 2 · Show only the last 4 digits

MaskPhone.javaJava
public class MaskPhone {    public static void main(String[] args) {        String phone = "9876543210";        String last4 = phone.substring(phone.length() - 4);        System.out.println("Number: XXXXXX" + last4);    }}
Output
Number: XXXXXX3210

What this code does

  • phone.length() - 4 is 10 - 4 = 6, so the cut starts at index 6.
  • The same line works for a number of any length; only the last 4 digits are shown.

To get the last n characters of any String, write s.substring(s.length() - n).

Find where to cut with indexOf()

The username of an email ends at the @, but every email is a different length. Find the cut point with indexOf() (opens in a new tab), then cut there.

Example 3 · Username and domain of an email

EmailParts.javaJava
public class EmailParts {    public static void main(String[] args) {        String email = "kavya.n@gmail.com";        int at = email.indexOf('@');        System.out.println("@ is at " + at);        System.out.println("User: " + email.substring(0, at));        System.out.println("Domain: " + email.substring(at + 1));    }}
Output
@ is at 7
User: kavya.n
Domain: gmail.com

What this code does

  • substring(0, at) stops just before the @, because the end index is not included.
  • at + 1 starts the domain one place after the @, so the @ itself is skipped.

Index rules

"Ravi".substring(2, 7) asks for characters that do not exist. Both numbers must be from 0 to length(), and start must not be bigger than end; otherwise the program stops with StringIndexOutOfBoundsException.

Java
String name = "Ravi";String a = name.substring(2, 4);   // "vi": end = length() is allowedString b = name.substring(2, 2);   // "" (empty)String c = name.substring(2, 7);   // StringIndexOutOfBoundsException

An end index equal to length() is fine, because the end is never read. An indexOf() result of -1 used as a cut point also stops the program, so check for -1 first.

Find the bug

Find the wrong cut

Select the line that is wrong.

This should print Beng.

Round 1 of 4

Next step

Check your understanding

9 quick questions