Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the length of a String without using the length method

 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
How to find the length of a String without using the length method.Please tell me.Thanks in advance.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to do that? Homework?

Have you looked at the API for java.lang.String? Did nothing occur to you?
 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
It was asked in an interview recently?Please give me the answer?Thanks
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and what did you answer in the interview ?
as Peter said: check out the API for java.lang.String

pascal
 
Prashant kumar Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you do not want to use length() method of string then treat String as array of Character .and use a loop.i think you will be known about it.
regards,
Prashant Kumar Singh
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to use a loop if you look at the methods available to String objects.

Personally, if I were asked this in an interview, I'd ask the interviewer in what circumstances you couldn't use the length method of a String. Its a silly question.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of that question probably wasn't to see how well you know the String API, but to see how you react to challenges.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic