public static int getLength(String str){
str = str.replaceAll( " [^x00-xff]* " , " ** " );
System.out.print(str);
return str.length();
}
public static void main(String[] args) throws UnsupportedEncodingException {
String str = " 我爱 " ;
// String gbStr = toLength(str, 11);
System.out.print(getLength(str));
}
打印结果为
****4