Java String substring() Method

The String.substring() method in Java is used to extract a portion of a string based on specified indices.

Table of Contents

  1. Introduction
  2. substring Method Syntax
  3. Examples
    • Extracting a Substring from a Starting Index
    • Extracting a Substring from a Starting Index to an Ending Index
    • Handling Edge Cases
    • Real-World Use Case
  4. Conclusion

Introduction

The String.substring() method is a member of the String class in Java. It allows you to extract a portion of a string based on specified starting and ending indices. This method is useful for string manipulation tasks, such as parsing and formatting.

substring() Method Syntax

The syntax for the substring method is as follows:

Extracting a Substring from a Starting Index

public String substring(int beginIndex)
  • beginIndex: The beginning index, inclusive.

Extracting a Substring from a Starting Index to an Ending Index

public String substring(int beginIndex, int endIndex)
  • beginIndex: The beginning index, inclusive.
  • endIndex: The ending index, exclusive.

Examples

Extracting a Substring from a Starting Index

The substring method can be used to extract a substring from a specified starting index to the end of the string.

Example

public class SubstringExample {
    public static void main(String[] args) {
        String str = "Welcome to Java Programming";
        String substr = str.substring(11);
        System.out.println("Substring from index 11: " + substr);
    }
}

Output:

Substring from index 11: Java Programming

Extracting a Substring from a Starting Index to an Ending Index

The substring method can also be used to extract a substring from a specified starting index to a specified ending index.

Example

public class SubstringExample {
    public static void main(String[] args) {
        String str = "Welcome to Java Programming";
        String substr = str.substring(11, 15);
        System.out.println("Substring from index 11 to 15: " + substr);
    }
}

Output:

Substring from index 11 to 15: Java

Handling Edge Cases

Example: Extracting a Substring from Index 0

If the starting index is 0, the substring method returns the original string.

public class SubstringFromZeroExample {
    public static void main(String[] args) {
        String str = "Java";
        String substr = str.substring(0);
        System.out.println("Substring from index 0: " + substr);
    }
}

Output:

Substring from index 0: Java

Example: Extracting a Substring with Equal Start and End Indices

If the starting and ending indices are equal, the substring method returns an empty string.

public class SubstringEqualIndicesExample {
    public static void main(String[] args) {
        String str = "Java";
        String substr = str.substring(2, 2);
        System.out.println("Substring from index 2 to 2: '" + substr + "'");
    }
}

Output:

Substring from index 2 to 2: ''

Example: Handling IndexOutOfBoundsException

If the indices are out of bounds, the substring method throws an IndexOutOfBoundsException.

public class SubstringOutOfBoundsExample {
    public static void main(String[] args) {
        String str = "Java";
        try {
            String substr = str.substring(5);
            System.out.println("Substring: " + substr);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: begin 5, end 4, length 4

Real-World Use Case

Example: Extracting a File Extension

One common use case for substring is extracting the file extension from a file name.

public class ExtractFileExtensionExample {
    public static void main(String[] args) {
        String fileName = "document.pdf";
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex != -1 && dotIndex != fileName.length() - 1) {
            String extension = fileName.substring(dotIndex + 1);
            System.out.println("File extension: " + extension);
        } else {
            System.out.println("No file extension found.");
        }
    }
}

Output:

File extension: pdf

In this example, the substring method is used to extract the file extension from the file name.

Conclusion

The String.substring() method in Java is used for extracting portions of a string based on specified indices. It provides a simple way to perform string manipulation tasks, making it useful for various applications such as parsing and formatting. By understanding and utilizing the substring method, you can efficiently manage string extraction tasks in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top