js判断中文字符串长度和js截取中文字符串
//获取字符串长度<br />String.prototype.strLen = function() { var len = 0; for (var i = 0; i < this.length; i++) { if (this.charCodeAt(i) > 255 || this.charCodeAt(i) <) len += 2; else len ++; } return len; } //将字符串拆成字符,并存到数组中 String.prototype.strToChars = function(){ var chars = new Array(); for (var i = 0; i < this.length; i++){ chars[i] = [this.substr(i, 1), this.isCHS(i)]; } String.prototype.charsArray = chars; return chars; } //判断某个字符是否是汉字 String.prototype.isCHS = function(i){ if (this.charCodeAt(i) > 255 || this.charCodeAt(i) <) return true; else return false; } //截取字符串(从start字节到end字节) String.prototype.subCHString = function(start, end){ var len = 0; var str = ""; this.strToChars(); for (var i = 0; i < this.length; i++) { if(this.charsArray[i][1]) len += 2; else len++; if (end < len) return str; else if (start < len) str += this.charsArray[i][0]; } return str; } //截取字符串(从start字节截取length个字节) String.prototype.subCHStr = function(start, length){ return this.subCHString(start, start + length); }<br /><br /><br />转:http://blog.csdn.net/clh604/article/details/8278303
相关推荐
wenmaomao 2014-06-03
东辉在线 2019-11-05
amyleeYMY 2017-07-13
t0ckh 2014-06-03
linkincsdn 2017-07-13
80183053 2013-07-19
卡奇莫多 2012-05-24
huanglianhuabj00 2011-09-30
么么茶技术网录 2017-03-13
编程的初衷是什么 2017-01-06
swywkci 2017-01-06
js网页特效 2016-12-16
fancyler 2019-05-07
js网页特效 2019-04-27
smilewgchao 2019-04-24
JING 2013-06-24
一像素 2013-04-17
ymeputer 2012-09-05
囩仴煳 2019-04-03