StringIndexOutOfBoundsException ☕ Java

java.lang.StringIndexOutOfBoundsException: begin 0, end 10, length 5

substring()/charAt() used an index outside the string.

Seen on: Java

Meaning

substring(begin, end) requires 0 ≤ begin ≤ end ≤ length. Short or empty input strings break fixed-length substrings.

Common causes

  • Fixed substring lengths on shorter input
  • Empty strings
  • indexOf returning -1 used as an index

⚡ Quick fix

  1. Check length first: s.substring(0, Math.min(10, s.length()))
  2. Handle indexOf == -1
  3. Validate input

Detailed fix by platform

Java

  1. String head = s.length() > 10 ? s.substring(0, 10) : s;

How to diagnose

  1. Indexes — begin/end/length in the message
  2. Input — Actual string value

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.