检查字符串是否与UTF-8兼容的mySQL [英] Checking if a string is UTF-8 compatible for mySQL
本文介绍了检查字符串是否与UTF-8兼容的mySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有更旧的mySQL DB,只支持UTF-8字符集。是否有一种方法在Java中检测给定的字符串是否与UTF-8兼容?
We have older mySQL DB that only support UTF-8 charset. Is a there a way in Java to detect if a given string will be UTF-8 compatible?
推荐答案
MySQL 定义:
名为utf8的字符集每个字符最多使用三个字节,并且只包含BMP字符。
The character set named utf8 uses a maximum of three bytes per character and contains only BMP characters.
因此,此函数应该工作:
Therefore this function should work:
private boolean isValidUTF8(final String string) {
for (int i = 0; i < string.length(); i++) {
final char c = string.charAt(i);
if (!Character.isBmpCodePoint(c)) {
return false;
}
}
return true;
}
这篇关于检查字符串是否与UTF-8兼容的mySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文