Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

Java - String substring() method example

0 contributions
5 points

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

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey đź‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more