Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:21:32+00:00 2026-06-15T16:21:32+00:00

Possible Duplicate: why from index is inclusive but end index is exclusive? substring() method

  • 0

Possible Duplicate:
why from index is inclusive but end index is exclusive?

substring() method => String substring(int beginIndex, int endIndex)

Description:

Returns a new string that is a substring of this string. The first
integer argument specifies the index of the first character. The
second integer argument is the index of the last character – 1.

Take a look at the following code:

String anotherPalindrome = "Niagara. O roar again!"; 
String roar = anotherPalindrome.substring(11, 15); 

Output: roar

Now, if the JVM didn’t substract int endIndex by one, we could just use substring(11,14) instead, wouldn’t that be much more convenient and less error-prone (human side)? Had you not read the description carefully, you might just ended up scratching your head for half an hour (like I did) when you thought that endIndex is just the normal index. What’s the reason for the Java language creators to subtract it by one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:21:33+00:00Added an answer on June 15, 2026 at 4:21 pm

    It has several advantages:

    s.substring(0, s.length())
    

    is always correct. Also:

    s.substring(i, j)
    

    The size of resulting string is always j - i. More specifically s.substring(0, j) will always return j characters. Finally this means that if you want to take n characters after index i you simply say:

    s.substring(i, i + n)
    

    It’s also easier to extract suffix of a string (last n characters):

    s.substring(s.length() - n, s.length())
    

    For example extracting file extension:

    s.substring(s.indexOf('.') + 1, s.length())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =
Possible Duplicate: how to remove index.php from url in codeigniter ? I want to
Possible Duplicate: best way to get value from Collection by index Say I have
Possible Duplicate: How to get Enum Value from index in Java? i have a
Possible Duplicate: passing index from for loop to ajax callback function (javascript) I've been
Possible Duplicate: Why am I getting Undefined index from my PHP? $pattern2 = /([A-Za-z0-9\.\-\_\!\#\$\%\&\'\*\+\/\=\?\^\`\{\|\}]+)\@([A-Za-z0-9.-_]+)(\.[A-Za-z]{2,5})/;
Possible Duplicate: passing index from for loop to ajax callback function (javascript) I'd like
Possible Duplicate: Get column index from label in a data frame I need to
Possible Duplicate: Avoid resending forms on php pages Index.html <form method=post action=demo.php> <input type=text
Possible Duplicate: Erasing from a std::vector while doing a for each? I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.