Extract a part of a string from the middle to the end.

Sponserd Links

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

In the previous article, we reviewed methods for extracting any single character from a string. It seems that this method can be used to extract a portion of a string, and there is such a method, String#substring() method. There are two methods available with the same name, but with different number of arguments. This is a mechanism called overloading. The name of the method is the same and the arguments are different. In this case, I used substring() with one argument.

Sponserd Links

Source and execution results

The substring(int index) method takes a portion of the string from the argument index to the end. In addition to this method, the length() method is used. This is a method to get the length of a string. The flow is described below.

  1. Initialize the string str with “Hello_Wordld.”.
  2. The following process is repeated until the repetition counter i is less than or equal to str.length() from 0.
    1. Use the printf() method to output “%2d:” + str.substring(i). Here, output the value of i in %2d.
    2. Call println().

Now, will there be an error if i=str.length()? Here is the source and execution result.

public class HelloSubstringBegin {

	public static void main(String[] args) {
		String str = "Hello_Wordld.";

		for (int i = 0; i <= str.length(); i++) {
			System.out.printf("%2d:" + str.substring(i), i);

Execution results

Was the result the same as you expected?


In the output, we use the printf() method, which is a method for passing the format as the first argument. I won’t go into the details, but in this usage, the second argument will be printed as a two-digit number in the %2d field. If the number is missing digits, it will be filled with spaces.

Inside the string object, strings are stored in a char type array. Its index is the argument of the substring() method. So if i is up to 12, the result will be satisfactory; if i=0, the output will be from the beginning to the end of the string.

Since the length of the string is 13, i=12 is exactly the last element number of the array. So, when i=12, the last character will be output. For the last i=13, what is output? It is an empty character. The following program confirms this.

public class HelloSubstringBeginEmpty {
    public static void main(String[] args) {

        String str = "Hello_Wordld.";

        if (str.substring(str.length()).isEmpty()) {

Execution results

The condition of the if statement was satisfied and the string “empty” was output. isEmpty() method determines whether the length of the string is zero or not and returns its boolean value. If the length is zero, true is returned. Anyway, no error will occur if you specify “string length” as the argument of the substring() method.

That’s all. I hope this is helpful to you.