Java Program to set a range for displaying substring

Java 8Object Oriented ProgrammingProgramming

Use the substring() method to set a range of substring from a string. Let’s say the following is our string.

String str = "pqrstuvw";

Getting the range of string from 3 to 6

String strRange = str.substring(3, 6);

The following is the complete example wherein we have set a range for displaying substring from a string.

Example

 Live Demo

public class Demo {
    public static void main(String[] args) {
       String str = "pqrstuvw";
       System.out.println("String: "+str);
       // range from 3 to 6
       String strRange = str.substring(3, 6);
       System.out.println("Substring: "+strRange);
    }
}

Output

String: pqrstuvw
Substring: stu
raja
Updated on 26-Jun-2020 16:10:40

Advertisements