This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes length function  for string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "length function  for string" Watch "length function  for string" New topic

length function for string

Rajesh Veluchamy
Ranch Hand

Joined: Jan 15, 2008
Posts: 47
For static data, the below code is fine,

but in runtime if the length of data is reduced, it throws exception
i have read something like length function

may be "data.length"

Thanks to all, in advance

-Rajesh.V<br /> Software Consultant
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

String have a length method. And arrays have a length property. See the JavaDocs for more.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44569
I think you should have given a bit more detail, Paul.

You could write "for(int i = 0; i < data.length; i++). . ." or "for(int i = 0; i < myString.length(); i++). . ."

but for an array, if you simply want to go through all the elements, use the "enhanced for" loop, (sometimes called the for-each loop), but you can't say "c = anything".
I agree. Here's the link:
subject: length function for string
It's not a secret anymore!