几个高效,简洁的字符处理函数

2019-06-02 22:42:31丽君

33 alert("2002-22-31 30:10:40".isDateTime())  //false 
34 */ 
35  
36  
37 // 检查 是否以特定的字符串结束 
38 String.prototype.startsWith=function(){ 
39     var _string=this 
40     return $A(arguments).any(function(value){return _string.slice(0,value.length)==value}) 
41 }; 
42 /* 
43 alert("http://www.google.com/".startsWith("http://","ftp://","telnet://"))  //true  满足其中任何一个就返回 true 
44 alert("http://www.google.com/".startsWith("https://","file://"))  //false 
45 alert("abc".startsWith("a"))  //true 
46 */ 
47  
48  
49 // 检查 是否以特定的字符串结束 
50 String.prototype.endsWith=function(){ 
51     var _string=this 
52     return $A(arguments).any(function(value){return _string.slice(value.length*(-1))==value}) 
53 }; 
54  
55  
56  
57 //从左边截取n个字符 ,如果包含汉字,则汉字按两个字符计算 
58 String.prototype.left=function(n){ 
59     return this.slice(0,n-this.slice(0,n).replace(/[x00-xff]/g,"").length) 
60 }; 
61 /* 
62 alert("abcdefg".left(3)==="abc") 
63 alert("中国人cdefg".left(5)==="中国") 
64 alert("中国abcdefg".left(5)==="中国a") 
65 */ 
66  
67  
68  
69  
70 //从右边截取n个字符 ,如果包含汉字,则汉字按两个字符计算 
71 String.prototype.right=function(n){ 
72     return this.slice(this.slice(-n).replace(/[x00-xff]/g,"").length-n) 
73 }; 
74  
75 /* 
76 alert("abcdefg".right(3)==="efg") 
77 alert("cdefg中国人".right(5)==="国人") 
78 alert("abcdefg中国".right(5)==="g中国") 
79 */ 
80  
81 </script>