1

We have a mySQL DB that only supports utf8. But we are getting some data feeds that require utf8mb4 for storing in mySQL. How can we detect (in Java) if a string will require utf8mb4 charset?

4

1 回答 1

5

需要 utf8mb4 的字符在 Java 中表示为代理对,占用 2 个字符。因此,检测它们的一种简单方法是检查 chars 中字符串的长度是否与代码点的数量相同:

boolean requiresMb4(String s) {
    int len = s.length();
    return len != s.codePointCount(0, len);
}
于 2014-01-31T10:42:07.487 回答