Java String substring() method


In this program, You will learn how to implement String substring() method in Java.


if (a.substring(b)) { }


Example: How to implement String substring() method in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String a, b;

        System.out.print("Enter String value :");
        a = sc.nextLine();

        b = a.substring(5);

        System.out.print("\nSubstring Value is :" + b);

    }
}

Output:

Enter String value :Welcome Java
Substring Value is :me Java


Java String substring() method with Index


In this program, You will learn how to implement String substring() method with Index in Java.


if (a.substring(index)) { }


Example: How to implement String substring() method with Index in Java.

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String a, b;

        System.out.print("Enter a string value :");
        a = sc.nextLine();

        b = a.substring(3, 6);

        System.out.print("\nSubstring value :" + b);

    }
}

Output:

Enter a string value :Welcome
Substring value :com