javasubstringbreakiterator

How to substring graphems from String?


I try to substring 5 graphems from String and cant make it properly. I have such String: πŸƒŽπŸ‚ΈπŸƒ…πŸƒ‹πŸƒπŸƒπŸƒŠ

My last try was with 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);
    }

Can anybody help me with this task?

Thank you in advance.


Solution

  • Your input contains special characters expressed in Unicode thus you should work with code points. See an example of a substringByCodepoint(String inputStr, int codePointStart, int codePointEnd) method implementation here.