• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Substrings

 
Ranch Hand
Posts: 373
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would "hello".substring(5) return?
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:What would "hello".substring(5) return?

What happens when you try it?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It ouputted 0
 
Ranch Hand
Posts: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:What would "hello".substring(5) return?

you have to provide start index and end index to which you want to get substring
Example...
"hello".substring(2, 4)
It will print "llo"as the output that means 2 is the start of index where 4 is the end of index
And if you are specifying only one index that is start index then you need have" hello world" then substring (5) will print after the index 5 which is "world" as output
Hope this helpful
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.

This method extracts the characters in a string between "start" and "end", not including "end" itself.



So, when you do something like it starts counting from 0 till 4 not 5 cause it not includes "end".

counting is like : h -> 0 , e -> 1, l -> 2, l -> 3, o -> 4 , so it will pick the word after the character "o" and, though there are no any other word or character after "hello" it returns nothing or 0.

 
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Java docs:

substring(int beginIndex)
Returns a new string that is a substring of this string.

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More importantly, from the substring examples on that page:
"emptiness".substring(9) returns "" (an empty string)

Now, I do question why a beginIndex that is the same as the length of the String returns an empty string, and all other longer indexes throw an exception.
I would have expected this case to throw an exception as well.

I must be missing something.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is how the method is defined. It is returning an n‑character String with the first n characters omitted. Which is of course different from returning null.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that was in response to me, then my question still stands.
Why does "hello".substring(5) return an empty string, and "hello".substring(6) an IndexOutOfBounds?

To me it's counter-intuitive.  5 is as much "out of bounds" as 6...
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it does seem counter‑intuitive for a substring() call to return a 0‑length String. But obviously whoever first wrote the substring() method thought differently. The link does show an example that returns "". Maybe it is like subsets, where you implicitly include the empty set, and the parent set is a subset of itself. Just as {} is a subset of every set, "" is a substring of every String.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:If that was in response to me, then my question still stands.
Why does "hello".substring(5) return an empty string, and "hello".substring(6) an IndexOutOfBounds?

To me it's counter-intuitive.  5 is as much "out of bounds" as 6...


Well, in which case:
"hello".substring(0, 5) also supposed to throw an out of bounds, but it doesn't, it returns hello.

I think it is sort of consistent here.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, you mentioned about beginIndex in case of one argument supplied. Ok, it could be confusing. Question for OCAJP?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:It ouputted 0

It returns a String. 0 is not a String. Do you mean a length of 0? An Empty String will have a length of 0

See String#substring()
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . you have to provide start index and end index to which you want to get substring

Please check the String documentation for details of overloaded methods before posting that sort of thing.

Example...
"hello".substring(2, 4)
It will print "llo" . . .

Afraid that isn't correct, as you will see from the appropriate documentation link.
 
sohail hussain
Ranch Hand
Posts: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

sohail hussain wrote:. . . you have to provide start index and end index to which you want to get substring

Please check the String documentation for details of overloaded methods before posting that sort of thing.

Example...
"hello".substring(2, 4)
It will print "llo" . . .

Afraid that isn't correct, as you will see from the appropriate documentation link.

I personally checked it compiled well but only difference is output is "ll" not "llo"
Because it take index less then one what you specified in substring(2,4) so on and counts index same as you specified that is on index 4 we have "l" so printed "ll"
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry; I misread you as having written "hello".substring(2, 4). That is all my mistake. Sorry.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic