Length of String in Java Without Using length() Method

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Length of String in Java Without Using length() Method | In the previous Java tutorial, we have seen how to find the length of a string in Java using the length() method. Method declaration, return type and many examples on length() method of String class.

But one question came into our mind:- Is it possible to find the length of a string in Java without using the length() function or method? Yes, we can. In this post, we will discuss how to find the Length of String in Java Without Using length() Method.

Using Char Array

Procedure to find the length of String in Java without using length() method but with the help of toCharArray() method.

a) Convert String to char array using toCharArray() method.
b) Use length property of array to display length of string or Iterate through the char array to count the length.

public class Test {
   public static void main(String[] args) {
      String str = "Java";
      
      // convert string to char array
      char[] ch = str.toCharArray();

      System.out.println("String = " + str);
      System.out.println("Length of String = " + ch.length);
      
      // find length
      int count = 0;
      for (char c : ch) {
         count++;
      }
      
      System.out.println("Length of String = " + count);
   }
}

Output:-

String = Java
Length of String = 4
Length of String = 4

In Java, every string literal ends with a ‘\0’ character which is also called a null character. For example, if we declare a string literal “Java” then it contains a total of 5 characters including the null character, and they are:- ‘J’, ‘a’, ‘v’, ‘a’, and ‘\0’. Internally these characters are stored as an array of characters or bytes.

The below two lines contains the same data,

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses
char[] ch = {'J', 'a', 'v', 'a', '\0'};
// is similar to 
String s4 = "Java";

Internally the string class converts the given string literal to an array of bytes or array of characters.

Using StringIndexOutOfBoundsException

When we fetch a character from the string and after reaching the end of the string it throws java.lang.StringIndexOutOfBoundsException

Using java.lang.StringIndexOutOfBoundsException we can find the length of the string in Java as follows:-

a) Use try block, fetch character of the string, and also count the length.
b) Once the end of the string is reached it will throw java.lang.StringIndexOutOfBoundsException, catch it.
c) After catching the exception control will come out of the try-catch block.
d) Return the count value.

Java program to find the length of the string in Java using StringIndexOutOfBoundsException

public class Test {
   public static void main(String[] args) {
      String str = "Know Program";
      
      System.out.println("String = " + str);
      System.out.println("Length of String = " + findLength(str));
   }

   public static int findLength(String str) {
      int i = 0;
      try {
         for(i=0; ; i++)
            str.charAt(i);
      } catch(Exception e) {
         // no operation
      }
      return i;
   }
}

Output:-

String = Know Program
Length of String = 12

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Mern Stack Web Development
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *