How to remove surrogate characters in Java?

前端 未结 5 766
时光说笑
时光说笑 2020-12-14 04:16

I am facing a situation where i get Surrogate characters in text that i am saving to MySql 5.1. As the UTF-16 is not supported in this, I want to remove these surrogate pai

5条回答
  •  死守一世寂寞
    2020-12-14 04:28

    Just curious. If char is high surrogate is there a need to check the next one? It is supposed to be low surrogate. The modified version would be:

    public static String removeSurrogates(String query) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < query.length(); i++) {
            char ch = query.charAt(i);
            if (Character.isHighSurrogate(ch))
                i++;//skip the next char is it's supposed to be low surrogate
            else
                sb.append(ch);
        }    
        return sb.toString();
    }
    

提交回复
热议问题