Differences between revisions 1 and 2
Revision 1 as of 2006-07-21 10:06:16
Size: 549
Editor: HoLin
Comment:
Revision 2 as of 2006-07-21 10:07:02
Size: 549
Editor: HoLin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
  {{{#!Javascript   {{{!#Javascript

JavaScript

  • 双字节字符串处理
    • {{{!#Javascript

String.prototype.leftB=function(len){

  • var s=this.replace(/\*/g," ").replace(/[^\x00-\xff]/g,"**") return this.slice(0,s.slice(0,len).replace(/\*\*/g," ").replace(/\*/g,"").length)

} String.prototype.lenB=function(){

  • return this.replace(/\*/g," ").replace(/[^\x00-\xff]/g,"**").length

} str="hi,我是中国人" alert("原始字符串:"+str+"\n字符串长度:"+str.length+"\n双字节长度:"+str.lenB()+"\n前10位字节:"+str.leftB(10))

  • }}}

HoLin/JavaScript (last edited 2009-12-25 07:12:30 by localhost)