forked from tugul/CoreJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMethods.java
More file actions
24 lines (21 loc) · 934 Bytes
/
Copy pathStringMethods.java
File metadata and controls
24 lines (21 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package strings;
/**
*
* String substring(int startIndex)
* String substring(int startIndex, int endIndex)
*
*/
public class StringMethods {
public static void main(String[] args) {
// startIndex is always inclusive
// 1. substring(int startIndex) -> inclusive start index till end
// 2. substring(int startIndex, int endIndex) -> inclusive start till exclusive end
String s = "hmm";
System.out.println(s.substring(1, 2)); // prints just 'm'
System.out.println(s.substring(1)); // prints 'mm'
System.out.println(s.substring(1, 3)); // prints 'mm'
System.out.println(s.substring(2, 1)); // Runtime exception : StringIndexOutOfBoundsException
System.out.println(s.substring(1, 4)); // Runtime exception : StringIndexOutOfBoundsException
// PS: substring on StringBuilder returns String, not StringBuilder object
}
}