Finding Length of the String without using inbuilt Java methods

Finding Length of the String without using inbuilt Java methods

Java is always preferred due to its inbuilt methods and packages which makes our task easier, but what if we are asked to do some task without using any particular inbuilt functions or methods.... So today on the demand of our visitors I am sharing my code to find the length of the String without using the length() method.
Finding Length of the String without using in-built Java methods

Introduction

The length of the String is the number of characters including white spaces in the string.
Java provides length() method which is used all the time to calculate the string length.
e.g:
String S= " Java Length Method "
int length= S.length();

Calculating String Length Without Using Inbuilt Methods

So now we are going to perform same task without using the length built-in method.

Logic and Explanation

We will create an static int function named strlen having one parameter which stores our source string and returns the calculated length of the string.
In the function body we used one integer variable len having its initial value 0. later we are using the try-catch block to perform the further operation ( you will get why its needed very soon)
Inside try block we have a while loop which will run infinitely (seriously?).
While loop body have charAt(  len ) method which is used to traverse the string as an Array for the index len, and we are incrementing integer variable len after that.
When the string completes, then  <string>.charAt() will throw an exception as array index out of range exception, Which is caught by the catch block which will do nothing (catch don't return anything) and the program will get-out from the try-catch block and returns the length of the string stored in the len.
e.g  s="Hello"
the charAt(len) will work till the len<=4 but when the len is 5 the charAt() will throws an exception.

Source Code

class StringLength {
    static  int strlen( String s ){
       int len=0;
       try{
           while(true){
               //traversing and counting
               s.charAt( len );
               len++;
           }
       } catch(Exception e){
           //Out of bounds
       }
        return len;
    }
    public static void main(String Args[]) {
        String text="Java String Length Function";
        System.out.println( "Length of String is " + strlen(text) );
    }
}

Conclusion

Finally we have the total length of the String return from the strlen function. Hope you got what's our approach on calculating the string length. If you have any problem in understanding the code or the logic, feel free to ask your problem the comment section below!
Please like and share this post and also subscribe to our newsletter to get our latest post directly into your mailbox.

3 Comments Leave new

thankuuuuuuuuuuuuuuuuuuuuuuuu soo much sir,,

Reply

Why This Function Throws an Exception

Reply

Isn't charAt a built-in method. I would like to see a code which calculates the length of string without using any built-in method of string. I have been trying it since days.

Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.