public class StringLength { public static void main(String[] args) { String str = "The cat in the hat"; int length = 0; try { for (int i = 0;; i++) { str.charAt(i); length++; } } catch (IndexOutOfBoundsException e) { /* * IndexOutOfBoundsException is caught when the index argument * is greater than the length of the string. */ } System.out.println("str: " + str); System.out.println("length: " + length); } }
$ java StringLength str: The cat in the hat length: 18
String length without using length() method Get length of a String without using the Java Built In length method. Finding the length of the String without using inbuilt Java methods