1String s = "Let's Get This Bread";
2
3String subString = s.substring(6, 9);
4 // (start index inclusive, end index exclusive)
5
6// subString == "Get"
1class Main {
2 public static void main (String[] args) {
3
4 String str = "Hello World!";
5
6 String firstWord = str.substring(0, 5);
7 //two parameters are start and end index: (inclusive, non-inclusive)
8
9 String secondWord = str.substring(6, 11);
10
11 //firstWord has string "Hello"
12 //secondWord has string "World"
13 }
14}
1class scratch{
2 public static void main(String[] args) {
3 String hey = "Hello World";
4 System.out.println( hey.substring(0, 5) );
5 // prints Hello;
6 }
7}
1import java.lang.*;
2
3public class StringDemo {
4public static void main(String[] args) {
5String str = "This is tutorials point";
6String substr = "";
7
8// prints the substring after index 8 till index 17
9substr = str.substring(8, 17);
10System.out.println("substring = " + substr);
11
12// prints the substring after index 0 till index 8
13substr = str.substring(0, 8);
14System.out.println("substring = " + substr);
15}
16}
1// substring(int begin, int end)
2String n = "hello there general kenobi";
3System.out.println(n.substring(6, 11));
4// -> "there"