Re: End of String
sara wrote:
Hi Guys,
I am very sorry to reply my messages and I know it is very not good.
Replying to others who have responded to you is the norm.
The reason for asking such a question is just curiosity. Because I know
in C++ we can easily implement strlen() function by checking each
character but in Java I could not get any good solution.
In Java it's very easy:
static int strlen(String str) {
return str.length();
}
Even more pointless, but not using length() per your original request :
static int strlen(String str) {
int i=0;
try {
while(true) {
str.getChar(i);
i++;
}
}
catch(IndexOutOfBoundsException) {
return i;
}
}
Are they enough to meet your requirements?
HINT: there is no need to implement a strlen "function" in Java because it is
not necessary in the way that it is in C/C++. A String in Java is an object,
and a part of that object is its own length, and a method to access it.
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555