I know there are various inbuilt functions, using which we can count string length like lastIndexOf()
, by converting a string into a char array and counting iteration. There is a way in C programming for(i = 0; s[i] != '\0'; ++i);
.
how to count string length in java like in C mentioned above.
Update:
Yes, there are these methods String.length()
, String.toCharArray()
. But if there are any other hard code way to find the string length.
String.toCharArray()
then count each iteration until you get anArrayIndexOutOfBoundsException
. But...why? – j.seashell'\0'
to the end of your string, and use the method from C. – RaghavlastIndexOf
doesn't provide length of array, but last index which holds specified value. "if there are any other hard code way to find the string length" what do you mean by hard code here? Can you include example of what you are searching and rationale explaining why / how you want to use it? – PshemoString
, you'll be calling built-in methods ofString
or ofStringBuilder
(in case of appending). – Ralf Kleberhoff