<script> function len(s) { var l = 0;var a = s.split(""); for (var i=0;i<a.length;i++) {if (a[i].charCodeAt(0)<299) {l++;} else { l+=2;}} return l; } var str = "你好啊"; alert(len(str)); </script>
方法(1):
public static void main(String[] args) {
String aString = "这是一个测试串aaa";
String anotherString = null;
try {
anotherString = new String(aString.getBytes("gbk"), "iso-8859-1");
} catch (UnsupportedEncodingException ex) {
}
System.out.println(aString.length() + "," + anotherString.length());
}
方法(2):
直接判断是否是中文是中文的话长度就是2,不是中文的话长度就是1