不使用length()方法的String的长度 [英] Length of the String without using length() method

查看:139
本文介绍了不使用length()方法的String的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用String类的 length()方法的情况下找到String的长度?

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

推荐答案


  • str.toCharArray()。length 应该有效。

    或者如何:

    str.lastIndexOf()

    甚至可能在恒定时间内运行:)

    Probably even runs in constant time :)

    另一个

    Matcher m = Pattern.compile("$").matcher(str);
    m.find();
    int length = m.end();
    


  • 最愚蠢的解决方案之一: str.split( ).length - 1

    这是作弊: new StringBuilder(str).length()? : - )

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

    这篇关于不使用length()方法的String的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆