package cn.com.songjy.test;
import java.io.UnsupportedEncodingException;
public
class
App
{
public
static
void main( String[] args ) throws UnsupportedEncodingException
{
String str =
"只"
;
System.out.println(getWordCount(str));
System.out.println(getWordCountRegex(str));
System.out.println(getWordCountCode(str,
"GBK"
));
System.out.println(getWordCountCode(str,
"UTF-8"
));
}
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;
}
public
static
int getWordCountRegex(String s)
{
s = s.replaceAll(
"[^\\x00-\\xff]"
,
"**"
);
int length = s.length();
return
length;
}
public
static
int getWordCountCode(String str, String code) throws UnsupportedEncodingException{
return
str.getBytes(code).length;
}
}