如何从字符串中提取字形?

How to substring graphems from String?

我尝试从 String 中提取 5 个字素,但无法正确完成。 我有这样的字符串:

我最后一次尝试使用 BreakIterator:

public String truncate(String input) {
        BreakIterator it = BreakIterator.getCharacterInstance();
        it.setText(input);
        String res = "";
        for(int i=0;i<5;i++)
            res += printAt(it,i,input);        
        return res;
    }

    public static String printAt(BreakIterator boundary, int pos, String source) {
        int end = boundary.following(pos);
        int start = boundary.previous();
        return source.substring(start,end);
    }

有人可以帮我完成这个任务吗?

提前致谢。

您的输入包含以 Unicode 表示的特殊字符,因此您应该使用代码点。 查看 substringByCodepoint(String inputStr, int codePointStart, int codePointEnd) 方法实现的示例 here