获取字符串字节数方法


//获取字符串字节方法1
public static int getWordCount(String s) {
int length = 0;
for (int i = 0; i < s.length(); i++) {
int ascii = Character.codePointAt(s, i);
if (ascii >= 0 && ascii <= 255)
length++;
else
length += 2;
}
return length;
}

//获取字符串字节方法2 正则表达式
public static int getWordCount2(String s) {
s = s.replaceAll("[^\\x00-\\xff]", "**");
int length = s.length();
return length;
}
posted @ 2022-05-19 10:47  丶Ronnie  阅读(678)  评论(0)    收藏  举报