- //第一种
- GetLength = function(str)
- {
- var realLength = 0;
- for (var i = 0; i < str.length; i++)
- {
- charCode = str.charCodeAt(i);
- if (charCode >= 0 && charCode <= 128)
- realLength += 1;
- else
- realLength += 2;
- }
- return realLength;
- }
- //第二种(采取将255意外的字符替换成"aa"的做法,取长度)
- GetLength = function(str)
- {
- return str.replace(/[^\x00-\xff]/g,"aa").length;
- };
- //第三种
- GetLength = function(s)
- {
- var len = 0;
- for(var i=0; i<s.length; i++)
- {
- var c = s.substr(i,1);
- var ts = escape(c);
- if(ts.substring(0,2) == "%u")
- {
- len+=2;
- } else
- {
- len+=1;
- }
- }
- return len;
- }
时间: 2024-11-08 10:09:35