惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
IT之家
IT之家
T
The Blog of Author Tim Ferriss
V
V2EX
博客园 - 聂微东
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
U
Unit 42
Vercel News
Vercel News
L
LangChain Blog
博客园 - 司徒正美
H
Help Net Security
Recent Announcements
Recent Announcements
Recorded Future
Recorded Future
V
Visual Studio Blog
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
Y
Y Combinator Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
F
Fortinet All Blogs
B
Blog
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
罗磊的独立博客
博客园_首页
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
量子位
I
InfoQ
小众软件
小众软件
P
Proofpoint News Feed

博客园 - Go_Rush

发一个python写的多线程 代理服务器 抓取,保存,验证程序,希望喜欢python的朋友和我一起完善它 根据生日或者日期 获取 生肖和星座的 JavaScript代码 身份证号码前六位所代表的省,市,区, 以及地区编码下载 15位, 18位的身份证号码的验证函数.以及根据身份证取省份,生日,性别 一个友好的.改善的 Object.prototype.toString的实现 发两个小东西,ASP/PHP 学习工具。 用JavaScript写的 怎样写一个通用的JavaScript效果库!(2/2) 怎样写一个通用的JavaScript效果库!(1/2) 无语,javascript居然支持中文(unicode)编程! ie 处理 gif动画 的onload 事件的一个 bug 讲两件事:1.this指针的用法小探. 2.ie的attachEvent和firefox的addEventListener在事件处理上的区别 总结两个Javascript的哈稀对象的一些编程技巧 使用prototype.js 的时候应该特别注意的几个问题. 再论怎么有效利用浏览器缓存之------怎么避免浏览器缓存静态文件. RE:对博客园URL的一些调整建议, 二级域名不利于客户端浏览器缓存 深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法 由 element.appendChild(newNode) ,谈开去, 分享几个并不常见的Dom操作技巧给大家 在项目中玩了把addRule,结果差点被它玩死。附 addRule在firefox下的兼容写法 JavaScript写作技巧,函数A中调用函数B, 怎样在函数B中写代码中断函数A的运行?
自己写的几个高效,简洁的字符处理函数
Go_Rush · 2006-12-28 · via 博客园 - Go_Rush

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

Posted on 2006-12-28 18:27  Go_Rush  阅读(3573)  评论()    收藏  举报

都是基于 String.prototype 的扩展:

 起因是有个网友和我讨论两个函数,

一个是 isDateTime (判断字符是否是符合 yyyy-mm-dd hh:mm:ss日期格式)
另一个是 left 函数,类似vbscript的left 实现中英文字符的混合截取。

他两个函数都用了循环,还用了N多 if 语句,每个函数都超过了40行代码,问我有无好的办法精简一下。
于是,我就写出了下面的代码,不敢说最效率最高,但是已经是够精简了, left函数才1行

 1 <script type="text/javascript">
 2 
 3 //by Go_Rush(阿舜) from http://ashun.cnblogs.com/
 4 
 5 function $A(arrayLike){
 6     for(var i=0,ret=[];i<arrayLike.length;i++) ret.push(arrayLike[i])
 7     return ret
 8 };
 9 Array.prototype.any=function(f){
10     for(var i=0;i<this.length;i++if (f(this[i],i,this)) return true;
11     return false
12 };
13 
14 
15 
16 //判断 字符串 是否符合 yyyy-mm-dd hh:mm:ss的日期格式, 格式正确而且闰年闰月等也要正确
17 
18 String.prototype.isDateTime=function(){  
19     try{
20         var arr=(this.length==19)?this.split(/\D/):[]
21         --arr[1]
22         eval("var d=new Date("+arr.join(",")+")")    
23         return     Number(arr[0])==d.getFullYear() && Number(arr[1])==d.getMonth() 
24                      && Number(arr[2])==d.getDate() && Number(arr[3])==d.getHours()
25                     && Number(arr[4])==d.getMinutes() && Number(arr[5])==d.getSeconds()
26     }catch(x){return false}
27 }
28 
29 /*
30 alert("2002-12-12 10:10:40".isDateTime())  //true
31 alert("2002-02-31 10:10:40".isDateTime())  //false
32 alert("2002-22-31 10:10:40".isDateTime())  //false
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>