How to remove surrogate characters in Java?

前端 未结 5 773
时光说笑
时光说笑 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:36

    if remove, all these solutions are useful but if repalce, below is better

    StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(Character.isHighSurrogate(c)){
                sb.append('*');
            }else if(!Character.isLowSurrogate(c)){
                sb.append(c);
            }
        }
        return sb.toString();
    

提交回复
热议问题