Extract a portion of a string


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 dealt with the String#substring() method with one argument. This time we will use the String#substring() method with two arguments. This method extracts a portion of a string. The usage is substring(int beginIndex, int endIndex).

The beginIndex is considered to be the same as the element number of the array. This method returns the characters up to the position of element number endIndex – 1. The second argument, endIndex, can be expressed in my own simple way: endIndex = beginIndex + [the number of characters to be retrieved].

In this example, we have written a program to extract two characters at a time.

Source and execution results

The flow is written below. I have included some common mistakes that can cause errors.

  1. Define a String-type variable str and initialize it with the string “Hello_World".
  2. Define a variable WIDTH of type final int and initialize it with 2.
    1. Repeat the following process until the repetition counter i is between 0 and less than str.length().
    2. Use the printf() method to output “%2d:" + str.substring(i, i+WIDTH). Output the value of variable i in %2d.
    3. Call println()

The following are the source and execution results.

	public static void main(String[] args) {
        String str = "Hello_World.";
        final int WIDTH = 2;

        for (int i = 0; i < str.length(); i++) {
            System.out.printf("%2d:" + str.substring(i, i+WIDTH), i);
Execution results
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 13
	at java.lang.String.substring(String.java:1963)
	at source0003.HelloSubstringBeginEnd.main(HelloSubstringBeginEnd.java:10)


Looking at the actual result, an error occurred when the value of the iteration counter variable i in the for statement was 11. It says “String index out of range: 13", so the error occurred when substring(11, 13) was executed. The number 13 is beyond the length of the string, and we know that such a specification is not possible.

In fact, according to the API specification, it is listed as endIndex is larger than the length of this String object. This is one of the conditions under which this method will throw an error. Since the range of the argument is specified in the condition of the exception, if you want to get the area around the end of the string, you must be careful not to raise this error.

By the way, I added final to WIDTH, which is used to declare a constant. Constants cannot be assigned. Writing a line that assigns a value to a constant will result in a compilation error.

It seems to be common practice to use all uppercase variable names for constants. It is also better to name them in such a way that the contents can be understood. In this case, is WIDTH good enough? I couldn’t think of any other appropriate word. If you have a better idea, use it.

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

Articles next and previous in the same category