Java String substring Method

Java String Substring Method

What is a string in Java?

In Java, a string object is an object that represents a sequence of characters. A string object is created using the String class, which is a built-in class in Java. Strings are used to represent text data in Java. We are going to learn all about Java String substring method.

  • The Java platform provides a rich set of classes and methods for working with strings, making it easy to perform common tasks such as searching, parsing, and formatting strings.

To know more about String Class in java read the complete article.

String substring() method.

The substring() method is a member of the String class in Java and is used to extract a portion of a string. The substring() method has two overloads: one that takes a single argument specifying the start index of the substring, and another that takes two arguments specifying the start index and end index of the substring.

  • The substring() method is useful when you want to extract a portion of a string and work with it as a separate string. It is commonly used in conjunction with other string manipulation methods such as indexOf(), lastIndexOf(), and replace().

Syntax:

public String substring(int beginIndex)

Parameters:

beginIndex - the beginning index, inclusive.

Let’s look at a string-related Java program where the Java String substring() Method is used to perform an operation on the given string.

Example: Java String substring() Method

Run
public class Main {
public static void main(String[] args) {
    String str = "Hello World";

    // Extract a substring starting at index 6 and ending at index 11
    String subString = str.substring(6, 11);

    // Print the length of the substring
    System.out.println("Length: " + subString.length());  // Outputs "Length: 5"

    // Check if the substring starts with a specific string
    System.out.println("Starts with 'W': " + subString.startsWith("W"));  // Outputs "Starts with 'W': true"

    // Check if the substring ends with a specific string
    System.out.println("Ends with 'd': " + subString.endsWith("d"));  // Outputs "Ends with 'd': true"

    // Get the character at a specific index in the substring
    char c = subString.charAt(2);
    System.out.println("Char at index 2: " + c);  // Outputs "Char at index 2: r"
}
}

Output

Length: 5
Starts with 'W': true
Ends with 'd': true
Char at index 2: r

Example 2 : Java String  substring() Method

Run
public class Main {
public static void main(String[] args) {
    String str = "Hello World";
    // Extract a substring starting at index 6 and ending at the end of the string
    String subString = str.substring(6);
    System.out.println(subString);
}
}

Output

World

Example 3: how to use the substring property:

Run
public class Main {
public static void main(String[] args) {
    String str = "Hello World";

    // Extract a substring starting at index 6 and ending at index 11
    String subString = str.substring(6, 11);

    // Print the length of the substring
    System.out.println("Length: " + subString.length());  // Outputs "Length: 5"

    // Check if the substring starts with a specific string
    System.out.println("Starts with 'W': " + subString.startsWith("W"));  // Outputs "Starts with 'W': true"

    // Check if the substring ends with a specific string
    System.out.println("Ends with 'd': " + subString.endsWith("d"));  // Outputs "Ends with 'd': true"

    // Get the character at a specific index in the substring
    char c = subString.charAt(2);
    System.out.println("Char at index 2: " + c);  // Outputs "Char at index 2: r"

    // Convert the substring to uppercase
    String upperCase = subString.toUpperCase();
    System.out.println("Uppercase: " + upperCase);  // Outputs "Uppercase: WORLD"
}
}

Output

Length: 5
Starts with 'W': true
Ends with 'd': true
Char at index 2: r
Uppercase: WORLD

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription