made function static to enable compiler optimization and moved

str.length() outside the loop to prevent unneeded method calls.