Java StringBuilder substring() Method

The StringBuilder.substring() method in Java is used to return a new string that is a substring of the current sequence. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. substring Method Syntax
  3. Examples
    • Extracting a Substring
    • Extracting a Substring with Only a Starting Index
    • Handling IndexOutOfBoundsException
  4. Conclusion

Introduction

The StringBuilder.substring() method is a member of the StringBuilder class in Java. It allows you to extract a portion of the character sequence from the StringBuilder and return it as a new String. This method is particularly useful when you need to work with a portion of the character sequence as a standalone string.

substring Method Syntax

The StringBuilder class provides two overloaded substring methods:

  1. substring(int start)
  2. substring(int start, int end)

Method 1: substring(int start)

The syntax for the first substring method is as follows:

public String substring(int start)
  • start: The starting index, inclusive.

Method 2: substring(int start, int end)

The syntax for the second substring method is as follows:

public String substring(int start, int end)
  • start: The starting index, inclusive.
  • end: The ending index, exclusive.

Examples

Extracting a Substring

The first substring method can be used to extract a portion of the character sequence starting from a given index until the end.

Example

public class StringBuilderSubstringExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        String subStr = sb.substring(7);

        System.out.println("Substring: " + subStr);
    }
}

Output:

Substring: World!

Extracting a Substring with Only a Starting Index

The second substring method can be used to extract a portion of the character sequence between two specified indices.

Example

public class StringBuilderSubstringExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        String subStr = sb.substring(7, 12);

        System.out.println("Substring: " + subStr);
    }
}

Output:

Substring: World

Handling IndexOutOfBoundsException

Attempting to use invalid indices will result in an IndexOutOfBoundsException. It's important to ensure that the specified range is within the valid bounds of the StringBuilder.

Example

public class StringBuilderSubstringExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        try {
            String subStr = sb.substring(7, 20); // This will throw an exception
            System.out.println("Substring: " + subStr);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: start 7, end 20, length 13

Conclusion

The StringBuilder.substring() method in Java is used for extracting a portion of the character sequence from a StringBuilder object and returning it as a new string. By understanding how to use the overloaded methods, you can efficiently work with substrings within your StringBuilder objects. Whether you need to extract a substring or handle potential exceptions, the substring method provides a reliable solution for these tasks.

Comments