String Class Tutorials

public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)

In this tutorial I will discuss the substring() method. It is important to understand that the first character of a string is located at the 0 index. The substring() method is overloaded and has two different signatures. The substring(int beginIndex) simply returns all remaining characters at and including the character at the specified index. For example:
String s = "0123456789";
String version1 = s.substring(4);
System.out.println("version1 = " + version1);
// version1 = 456789

The substring(int beginIndex, int endIndex) is a little more complicated and I find the way the second parameter functions somewhat unusual. The first parameter instructs the substring method to begin returning characters at and including the character at the specified index. The second parameter instructs the substring method to stop returning characters excluding the character at the specified index. For example:
String t = "0123456789";
String version2 = t.substring(4, 8);
System.out.println("version2 = " + version2);
// version2 = 4567
When thinking through how the substring works it is a little confusing to imagine something like this ... I want all the characters at index 4, 5, 6, and 7. It makes sense to just use substring(4, 7), but I have to remember that the second parameter does not work the same way as the first and so I must add 1 to index that I really want. The way that I remember the difference is to think of it like this ... if I want 4 characters I need to specify the starting character and add 4 to get the second parameter. I can tell you that the functionality is strange compared to other languages, but it is what it is.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StringSubstring
C:\Java>cd StringSubstring
C:\Java\StringSubstring>Notepad StringSubstring.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class StringSubstring {
    public static void main(String args[]) {
        String s = "0123456789";
        String version1 = s.substring(4);
        System.out.println("version1 = " + version1); 

        String version2 = s.substring(4, 8);
        System.out.println("version2 = " + version2); // version2 = 4567

        //String error = s.substring(8, 12); // out of bounds exception
        //String error = s.substring(-1, 4); // out of bounds exception
        //String error = s.substring(8, 4); // out of bounds exception
    }
}


Now switch back to the command prompt (CMD) and type in javac StringSubstring.java and press Enter.
Now type in java StringSubstring and press Enter.


C:\Java\StringSubstring>javac StringSubstring.java
C:\Java\StringSubstring>java StringSubstring
456789
4567
various Exceptions


Final thoughts

When using the substring method with two parameters, it is important to remember that the first parameter instructs the substring method to begin returning characters at and including the character at the specified index. The second parameter instructs the substring method to stop returning characters excluding the character at the specified index. One strategy that I regularly use to solidify my knowledge of Java is to browse through the actual source code. Due to some language in their copyright file, I can't actually show the source code in my videos. The source code is contained inside of a file called src.zip located in the folder where your JDK is installed. I recommend using notepad++ to browse the class files.


Tutorials