Languages
[Edit]
EN

Java - String substring() method example

5 points
Created by:
Kate_C
2857

Short example:

//     index - 012
String text = "abc";

System.out.println(  text.substring(0)  ); // abc
System.out.println(  text.substring(1)  ); // bc
System.out.println(  text.substring(2)  ); // c
System.out.println(  text.substring(3)  ); //

System.out.println(  text.substring(0, 0)  ); //
System.out.println(  text.substring(0, 1)  ); // a
System.out.println(  text.substring(0, 2)  ); // ab
System.out.println(  text.substring(1, 2)  ); // b
System.out.println(  text.substring(1, 3)  ); // bc
System.out.println(  text.substring(0, 3)  ); // abc
System.out.println(  text.substring(2, 3)  ); // c
System.out.println(  text.substring(3, 3)  ); //

In this post we cover usage of String substring() method with simple code examples.

We use Java String substring() method to get substring of given string based on provided indexes.

1. String substring(int beginIndex)

String substring with single parameter example. 

int beginIndex - begin index is inclusive.

public class Example1 {

    public static void main(String[] args) {

        //    index - 012
        String str = "abc";

        System.out.println(str.substring(0)); // abc
        System.out.println(str.substring(1)); // bc
        System.out.println(str.substring(2)); // c
        System.out.println(str.substring(3)); //
    }
}

Output:

abc
bc
c

2. String substring(int beginIndex, int endIndex)

String substring with 2 parameters example.

int beginIndex - begin index is inclusive

int endIndex    - end    index is exclusive

public class Example2 {

    public static void main(String[] args) {

        //    index - 012
        String str = "abc";

        System.out.println(str.substring(0, 0)); //
        System.out.println(str.substring(0, 1)); // a
        System.out.println(str.substring(0, 2)); // ab
        System.out.println(str.substring(1, 2)); // b
        System.out.println(str.substring(1, 3)); // bc
        System.out.println(str.substring(0, 3)); // abc
        System.out.println(str.substring(2, 3)); // c
        System.out.println(str.substring(3, 3)); //
    }
}

Output:


a
ab
b
bc
abc
c

3. String substring(int beginIndex, int endIndex) - more complex example

public class Example3 {

    public static void main(String[] args) {

        //    index - 0123456789
        String str = "abcdefghij";

        System.out.println(str.substring(0, 1)); // a
        System.out.println(str.substring(0, 2)); // ab
        System.out.println(str.substring(1, 2)); // b
        System.out.println(str.substring(1, 3)); // bc
        System.out.println(str.substring(0, 3)); // abc
        System.out.println(str.substring(0, 4)); // abcd
        System.out.println(str.substring(4, 7)); // efg
        System.out.println(str.substring(4, str.length())); // efghij
        System.out.println(str.substring(0, str.length())); // abcdefghij

        // System.out.println(str.substring(-1, str.length()));
        // when we use -1 index we will get below exception:
        // StringIndexOutOfBoundsException: String index out of range: -1
    }
}

Output:

a
ab
b
bc
abc
abcd
efg
efghij
abcdefghij

References

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.