Length of String in Java Without Using length() Method

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 of 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.

Length of String in Java Using Char Array

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

  • Convert String to char array using toCharArray() method.
  • Use the length property of the array to display the length of the 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 contain the same data,

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:-

  • Use try block, fetch the character of the string, and also count the length.
  • Once the end of the string is reached it will throw java.lang.StringIndexOutOfBoundsException, catch it.
  • After catching the exception control will come out of the try-catch block.
  • 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 do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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