Spaces:
Running
Running
Create 12a. Substrings
Browse files
Week 2: Methods, strings and lists/12a. Substrings
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
s.substring(int initialindex), which returns a substring of the string s FROM the GIVEN INDEX TO THE END of the string
|
| 2 |
+
|
| 3 |
+
s.substring(int startindex, int endindex), which returns the substring 's' of the string BETWEEN the given INDICES
|
| 4 |
+
- from startindex
|
| 5 |
+
- UP TO BUT NOT INCLUDING endindex
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Examples on using substrings:
|
| 12 |
+
String str = "abcdefghijk";
|
| 13 |
+
|
| 14 |
+
// five first characters
|
| 15 |
+
System.out.println(str.substring(0,5));
|
| 16 |
+
|
| 17 |
+
// Characters from third character onward
|
| 18 |
+
System.out.println(str.substring(2));
|
| 19 |
+
|
| 20 |
+
// Characters 3-6
|
| 21 |
+
System.out.println(str.substring(3, 7));
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
Program outputs:
|
| 25 |
+
abcde
|
| 26 |
+
cdefghijk
|
| 27 |
+
defg
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|