欢迎来到天天文库
浏览记录
ID:19395756
大小:71.00 KB
页数:19页
时间:2018-10-01
《js字符串方法10067》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、js字符串方法10067js字符串方法.txt3努力奋斗,天空依旧美丽,梦想仍然纯真,放飞自我,勇敢地飞翔于梦想的天空,相信自己一定做得更好。4苦忆旧伤泪自落,欣望梦愿笑开颜。5懦弱的人害怕孤独,理智的人懂得享受孤独js字符串方法2010年01月11日星期一09:47JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串。vara="hello";varb=",world";varc=a.concat(b);alert(c);//c="hello,world"indexOf返回字符串中一个子串第一处出现的索引(
2、从左到右搜索)。如果没有匹配项,返回-1。varindex1=a.indexOf("l");//index1=2varindex2=a.indexOf("l",3);//index2=3charAt返回指定位置的字符。varget_char=a.charAt(0);//get_char="h"lastIndexOf返回字符串中一个子串最后一处出现的索引(从右到左搜索),如果没有匹配项,返回-1。varindex1=lastIndexOf('l');//index1=3varindex2=lastIndexOf('l',2)//in
3、dex2=2match检查一个字符串匹配一个正则表达式内容,如果么有匹配返回null。varre=newRegExp(/^w+$/);varis_alpha1=a.match(re);//is_alpha1="hello"varis_alpha2=b.match(re);//is_alpha2=nullsubstring返回字符串的一个子串,传入参数是起始位置和结束位置。varsub_string1=a.substring(1);//sub_string1="ello"varsub_string2=a.substring(1,4
4、);//sub_string2="ell"substr返回字符串的一个子串,传入参数是起始位置和长度varsub_string1=a.substr(1);//sub_string1="ello"varsub_string2=a.substr(1,4);//sub_string2="ello"replace用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。varresult1=a.replace(re,"Hello");//result1="Hello"varresult2=b.replace(re,"Hello
5、");//result2=",world"search执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回-1。varindex1=a.search(re);//index1=0varindex2=b.search(re);//index2=-1slice提取字符串的一部分,并返回一个新字符串(与substring相同)。varsub_string1=a.slice(1);//sub_string1="ello"varsub_string2=a.slice(1,4);//sub_string2="ell"s
6、plit通过将字符串划分成子串,将一个字符串做成一个字符串数组。vararr1=a.split("");//arr1=[h,e,l,l,o]length返回字符串的长度,所谓字符串的长度是指其包含的字符的个数。varlen=a.length();//len=5toLowerCase将整个字符串转成小写字母。varlower_string=a.toLowerCase();//lower_string="hello"toUpperCase将整个字符串转成大写字母。varupper_string=a.toUpperCase();//up
7、per_string="HELLO"/*******************************************字符串函数扩充*******************************************//*===========================================//去除左边的空格===========================================*/String.prototype.LTrim=function(){returnthis.replace(/(^
8、s*)/g,"");}/*===========================================//去除右边的空格===========================================*/String.protot
此文档下载收益归作者所有