Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, December 6, 2021

Java String substring() Method Example

1. Introduction


In this tutorial, You'll learn how to get the portion of a string using the substring() method of String API.

The name of this method indicates that it will fetch substring from an original string for a given index.

Java String substring() Method Example

String program to find the regionmatches() method.

2. Java String Substring Syntax


This is an overloaded method in two versions which takes only the start index and the other takes two arguments for the start and end index.

public String substring​(int beginIndex)
public String substring​(int beginIndex, int endIndex)

Note: Start index is included and the end index is not excluded in the substring result. As usual as all string methods, this also returns a new String with substring content stored in the string constant pool.

3. String Substring Example 1 - substring​(int beginIndex)


Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Example java program to fetch the account status from general content.

package com.javaprogramto.w3schools.programs.string;

public class StringSubStringExample {

    public static void main(String[] args) {

        String accountInfo = "In Active";

        String accountStatus = accountInfo.substring(3);

        System.out.println("Extracted account status from account info text for account 1: "+accountStatus);

        accountInfo = "Is Locked";

        accountStatus = accountInfo.substring(3);

        System.out.println("Extracted account status from account info text for account 2: "+accountStatus);
        
    }
}

Output:

Extracted account status from account info text for account 1:  Active
Extracted account status from account info text for account 2:  Locked


4. String Substring Example 2 - substring​(int beginIndex, int endIndex)


Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

package com.javaprogramto.w3schools.programs.string;

public class StringSubEndIndexStringExample {

    public static void main(String[] args) {

        String accountInfo = "Is Active my account?";

        String accountStatus = accountInfo.substring(3, 10);

        System.out.println("Extracted account status from account info text for account 1: "+accountStatus);

        accountInfo = "is my Locked ?";

        accountStatus = accountInfo.substring(6, 12);

        System.out.println("Extracted account status from account info text for account 2: "+accountStatus);

    }
}

Output:

Extracted account status from account info text for account 1: Active

Extracted account status from account info text for account 2: Locked

5. Conclusion


In this article, We've seen how to use the String class substring() method to get some part of it.

You can get from an index to the end of the string or between two indexes content.

As usual, all programs are shown in this article are over GitHub.

Reference

No comments:

Post a Comment

Please do not add any spam links in the comments section.