js回归之字符串
Funny things are similar things confusing you...
字符串在I/O起着媒介的作用,他的作用不可小瞧,所哟在各个语言体系中,String非常有意思。
大致方法:
1.字符:charAt,charcodeAt
=============================================================================================
2.寻找字符串:slice,substring,substr
这三个最应开头的第一句话:哈哈,slice(indexA,indexB)substring(indexA,indexB),substr(indexA,length)
其中substring,substr最容易混淆,我是这样记得(sunstring with ING so it must be reference)
给个例子三个的区别自然懂了:
var s="hello world";
s.slice(3);//lo world
s.substring(3) //lo world
s.substr(3)//lo world
s.slice(3,7)//lo w
s.substring(3,7)//lo w
s.substr(3,7)//lo worl
值得注意的是,当第二个参数是负数的时候,三个的表现就差别挺大的 slice是负数加上字符串长度(最正常的),substring是吧所有负数变为0,substr第一个参数负数加上字符串长度,第二个参数为0
s.slice(-3)//rld
s.substring(-3)//hello world
s.substr(-3)//rld
s.slice(3,-4)//lo w
s.substring(3,-4)//hel
s.substr(3,-4)//""
=============================================================================================
3.位置方法
indexof ->往后搜索
lastindexof <- 往前搜索
result=indexof(str),if result=-1 表明没有找到
("string",searchindex)第二个参数即表示要从哪个位置开始搜索,很简单不多讲
4.大小写 uppercase,lowercase
5.trim 去除两边空格
6.模式匹配这个作为重点灵气一章节