June 2018
Beginner
722 pages
18h 47m
English
Various int indexOf(String str) and int lastIndexOf(String str) methods return the position of a substring in a string:
String s = "Introduction";System.out.println(s.indexOf("I")); //prints: 0System.out.println(s.lastIndexOf("I")); //prints: 0System.out.println(s.lastIndexOf("i")); //prints: 9System.out.println(s.indexOf("o")); //prints: 4System.out.println(s.lastIndexOf("o")); //prints: 10System.out.println(s.indexOf("tro")); //prints: 2
Notice that the position count starts from zero.
The method String substring(int beginIndex) returns the rest of the string value, starting from the position (index) passed in as the parameter:
String s = "Introduction";System.out.println(s.substring(1)); //prints: ntroduction ...
Read now
Unlock full access