File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 45365
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!