harper.lisa16
harper.lisa16 5d ago • 0 views

Java String .substring() method explained with examples

Hey everyone! 👋 I'm trying to get my head around the Java `String.substring()` method. It seems super useful for manipulating text, but sometimes I get confused with the start and end indices. Can someone help me understand it better, maybe with some examples and a quick quiz to test my knowledge? 🙏
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer
User Avatar
Picasso_Stroke Mar 16, 2026

📚 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()

  1. 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"

  2. Question 2:

    Consider `String text = "JavaProgramming";`. What does `text.substring(4, 8);` return?

    A) "Progr"

    B) "Prog"

    C) "aPro"

    D) "aProgr"

  3. Question 3:

    If `String data = "eokultv";`, what is the result of `data.substring(0, 3);`?

    A) "eok"

    B) "eoku"

    C) "okul"

    D) "e"

  4. 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);`

  5. Question 5:

    What is the length of the string returned by ` "Programming".substring(3, 7);`?

    A) 3

    B) 4

    C) 7

    D) 10

  6. 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"

  7. 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:

  1. Question 1: A) "World" (Index 6 is 'W')
  2. Question 2: B) "Prog" (Indices 4, 5, 6, 7 are 'P', 'r', 'o', 'g')
  3. Question 3: A) "eok" (Indices 0, 1, 2 are 'e', 'o', 'k')
  4. Question 4: C) `str.substring(2, 1);` (`beginIndex` must be less than or equal to `endIndex`)
  5. Question 5: B) 4 ($endIndex - beginIndex$ = 7 - 3 = 4)
  6. Question 6: B) "Java" (`indexOf("Java")` is 9, `indexOf(" is")` is 13. `substring(9, 13)` gives "Java")
  7. 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 In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀