1 Answers
📚 Quick Study Guide: Java String .substring()
- 🎯 Purpose: The `substring()` method in Java is used to extract a portion of a string, creating a new string from a sequence of characters within the original.
- 🛠️ Two Overloaded Versions:
- 📝 `substring(int beginIndex)`: Returns a new string that is a substring of this string. The substring begins with the character at the specified `beginIndex` and extends to the end of this string.
- ✂️ `substring(int beginIndex, int endIndex)`: Returns a new string that is a substring of this string. The substring begins at the specified `beginIndex` and extends to the character at `endIndex - 1`. The character at `endIndex` is not included.
- 🔢 Zero-Based Indexing: Like arrays, Java strings use zero-based indexing. The first character is at index 0, the second at index 1, and so on.
- ✅ Inclusive `beginIndex`: The character at `beginIndex` is always included in the resulting substring.
- 🛑 Exclusive `endIndex`: The character at `endIndex` is always *excluded*. Think of `endIndex` as "up to, but not including."
- 📏 Length Calculation: The length of the substring created by `substring(beginIndex, endIndex)` is $endIndex - beginIndex$.
- ⚠️ `IndexOutOfBoundsException`: This exception is thrown if `beginIndex` is negative, greater than `length()`, or if `beginIndex` is greater than `endIndex`, or if `endIndex` is negative or greater than `length()`.
- 💡 Common Use Cases: Extracting parts of file names, parsing data, truncating strings.
🧠 Practice Quiz: Java String .substring()
-
Question 1:
Given the string `String s = "Hello World";`, what will be the output of `s.substring(6);`?
A) "World"
B) " World"
C) "o World"
D) "Hello"
-
Question 2:
Consider `String text = "JavaProgramming";`. What does `text.substring(4, 8);` return?
A) "Progr"
B) "Prog"
C) "aPro"
D) "aProgr"
-
Question 3:
If `String data = "eokultv";`, what is the result of `data.substring(0, 3);`?
A) "eok"
B) "eoku"
C) "okul"
D) "e"
-
Question 4:
Which of the following method calls would throw an `IndexOutOfBoundsException` for `String str = "Example";`?
A) `str.substring(0, 7);`
B) `str.substring(7);`
C) `str.substring(2, 1);`
D) `str.substring(3, 3);`
-
Question 5:
What is the length of the string returned by ` "Programming".substring(3, 7);`?
A) 3
B) 4
C) 7
D) 10
-
Question 6:
Given `String sentence = "Learning Java is fun";`, what does `sentence.substring(sentence.indexOf("Java"), sentence.indexOf(" is"));` produce?
A) "Java is"
B) "Java"
C) "Java "
D) "Java i"
-
Question 7:
If `String val = "Developer";`, what is the output of `val.substring(val.length() - 3);`?
A) "per"
B) "oper"
C) "eloper"
D) "Dev"
Click to see Answers
Answer Key:
- Question 1: A) "World" (Index 6 is 'W')
- Question 2: B) "Prog" (Indices 4, 5, 6, 7 are 'P', 'r', 'o', 'g')
- Question 3: A) "eok" (Indices 0, 1, 2 are 'e', 'o', 'k')
- Question 4: C) `str.substring(2, 1);` (`beginIndex` must be less than or equal to `endIndex`)
- Question 5: B) 4 ($endIndex - beginIndex$ = 7 - 3 = 4)
- Question 6: B) "Java" (`indexOf("Java")` is 9, `indexOf(" is")` is 13. `substring(9, 13)` gives "Java")
- Question 7: A) "per" (`val.length()` is 9. `val.length() - 3` is 6. `substring(6)` is "per")
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀