Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Substrings  RSS feed

 
Ranch Hand
Posts: 208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would "hello".substring(5) return?
 
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It ouputted 0
 
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.

 
Ranch Foreman
Posts: 242
3
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Java docs:

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

 
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • 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
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • 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: 43
1
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry; I misread you as having written "hello".substring(2, 4). That is all my mistake. Sorry.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!