Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

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

like image 766
Rahul Avatar asked May 26 '10 05:05

Rahul


1 Answers

  • 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()? :-)

like image 75
aioobe Avatar answered Sep 23 '22 19:09

aioobe