Java String中的字符数

dat*_*ush 4 java unicode utf-8 thai

可能重复:
Java:使用unicode overline显示平方根时的字符串长度?

如何获取String中的Unicode字符数?

给出一个char[]泰国字符:

[?, ?, ?, ?, ?, ?, ?]
Run Code Online (Sandbox Code Playgroud)

这在String中出现:อภิชาติ

String.length() 返回7.我知道(技术上)有7个字符,但是我需要一个能够返回5的方法.这就是屏幕上显示的字符空间的确切数量.

Joa*_*son 5

似乎你只是想不将unicode标记统计为单独的字符;

static boolean isMark(char ch)
{
    int type = Character.getType(ch);
    return type == Character.NON_SPACING_MARK ||
           type == Character.ENCLOSING_MARK ||
           type == Character.COMBINING_SPACING_MARK;
}
Run Code Online (Sandbox Code Playgroud)

可以用作;

String olle = "???????";
int count = 0;

for(int i=0; i<olle.length(); i++)
{
    if(!isMark(olle.charAt(i)))
        count++;
}

System.out.println(count);
Run Code Online (Sandbox Code Playgroud)

并返回'5'.