Length of the String without using length() method [closed]

How can I find the length of a String without using the length() method of String class?


  • str.toCharArray().length should work.

  • Or how about:

    str.lastIndexOf("")

    Probably even runs in constant time :)

  • Another one

    Matcher m = Pattern.compile("$").matcher(str);
    m.find();
    int length = m.end();
    
  • One of the dumbest solutions: str.split("").length - 1

  • Is this cheating: new StringBuilder(str).length()? :-)


String blah = "HellO";
int count = 0;
for (char c : blah.toCharArray()) {
    count++;
}
System.out.println("blah's length: " + count);