Size: 549
Comment:
|
Size: 543
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
{{{!#Javascript | {{{#!Java |
JavaScript
- 双字节字符串处理
1 String.prototype.leftB=function(len){ 2 var s=this.replace(/\*/g," ").replace(/[^\x00-\xff]/g,"**") 3 return this.slice(0,s.slice(0,len).replace(/\*\*/g," ").replace(/\*/g,"").length) 4 } 5 String.prototype.lenB=function(){ 6 return this.replace(/\*/g," ").replace(/[^\x00-\xff]/g,"**").length 7 } 8 str="hi,我是中国人" 9 alert("原始字符串:"+str+"\n字符串长度:"+str.length+"\n双字节长度:"+str.lenB()+"\n前10位字节:"+str.leftB(10))