How to correctly compute the length of a String in Java?

后端 未结 5 810
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 02:54

I know there is String#length and the various methods in Character which more or less work on code units/code points.

What is the suggested

5条回答
  •  囚心锁ツ
    2020-12-08 03:45

    String.length() does not return the size of the array backing the string, but the actual length of the string, defined as "the number of Unicode code units in the string." (see API docs).

    (As pointed out by Stephen C in the comments, Unicode code units == Java chars)

    If this is not what you are looking for, then perhaps you should elaborate the question a bit more.

提交回复
热议问题