检测utf8mb4字符集要求 [英] Detecting a utf8mb4 charset requirement

查看:617
本文介绍了检测utf8mb4字符集要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个只支持utf8的mySQL DB。但是我们正在获得一些需要utf8mb4存储在mySQL中的数据源。我们如何检测(在Java中)字符串是否需要utf8mb4字符集?

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?

推荐答案

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

Characters that require utf8mb4 are represented as a surrogate pair in Java, and occupy 2 chars. A simple way to detect them is therefore checking if the length of the string in chars is the same as the number of code points:

boolean requiresMb4(String s) {
    int len = s.length();
    return len != s.codePointCount(0, len);
}

这篇关于检测utf8mb4字符集要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆