JS内置对象(String/字符串对象)

[color=red][size=large]Javascript中,对象是数据,拥有属性与方法[/size][/color]
[size=x-large][b]字符串(String)对象[/b][/size]
[b][size=large]1.创建string对象的语法:(2种)[/size][/b]
var str1=new String("hello");
var str2=String("world");

[b][size=large]2.属性[/size][/b]
[*]访问对象属性的语法:objectName.propertyName
[*][color=orange][size=large]length[/size][/color]:表示字符串的长度
[*]访问:
var x=str1.length;
console.debug(x);//5

[b][size=large]3.方法(部分)[/size][/b]
[size=large]3.1查询方法[/size]
[list]
[*][color=orange][size=large]charAt(index)[/size] [/color]
返回在指定位置的字符,可根据索引位置查找字符
[*][color=orange][size=large]charCodeAt(index) [/size][/color]
返回在指定位置的字符的Unicode编码,可根据索引位置查找字符编码
[*][color=orange][size=large]indexOf(searchvalue,fromindex) [/size][/color]
检索字符串,根据字符(串)查找首次出现的位置,如果找不到则返回-1;
不指定第二个起始位置时从头开始查找,如果指定则从指定位置开始查找;
searchvalue必需,规定需检索的字符串值;
fromindex可选,表示在字符串中开始检索的位置
[*][color=orange][size=large]lastindexOf(searchvalue,fromindex)[/size] [/color]
从后向前搜索字符串,根据字符(串)查找最后一次出现位置
[/list]
		var str="HelloWorld";
var ch1=str.charAt(5);//W,等同于str[5]
var ch2=str.charCodeAt(1);//101 e 的ASCII码为101
console.debug("ch1="+ch1+";typeof ch1:"+typeof ch1);//string
console.debug("ch2="+ch2+";typeof ch1:"+typeof ch2);//number

var ret=str.indexOf("o");//4
console.debug("ret="+ret+";typeof ret:"+typeof ret);//number
ret=str.lastIndexOf("o");//6
console.debug("ret="+ret);
console.debug("--------");
// 查找出下列字符串中所有字母o出现的位置
var ss="HeoloeWorldGropud";
ret=-1;
while(true){
ret=ss.indexOf("o",ret+1);
//ret+1表示从第一个o出现的位置的下一个位置开始检索
if(ret==-1){
break;
}
console.debug("o in "+ret);
}


后台运行结果:

[img]http://dl2.iteye.com/upload/attachment/0118/0625/3fc3944b-d70a-3043-871e-d3c45e9c831a.png[/img]
[size=large]3.2大小写转换[/size]
[size=large][color=orange]toLowerCase()[/color][/size]
stringObject.toLowerCase(),将字符串中所有大写字符全部转换成小写字符
[size=large][color=orange]toUpperCase()[/color][/size]
stringObject.toUpperCase(),将字符串中所有小写字符全部转换成大写字符
[size=large]3.3截取方法[/size]
[color=orange][size=large]substr(star,length)[/size][/color]
star必需,为数值,表示起始位置,如为负数,那么位置从字符串的尾部开始算起(倒数)
length可选,数值,表示截取的数目,如没有,表示从起始位置到字符串结尾
[color=orange][size=large]substring(start,stop)[/size][/color]
start必需,非负整数,表示起始位置,不支持倒数
stop可选,非负整数,结束位置,截取的是结束位置的前一个字符
start与stop大小想法有自动切换的能力
[color=orange][size=large]slice(start,end)[/size][/color]
start起始位置,支持倒数;
end结束位置,可选,支持倒数,如未提供,截取到结束
slice无自动交换能力
	var str="HelloWorld";
var s1=str.substr(-5);//World
var s2=str.substr(5,3);//Wor
console.debug("str:"+str);
console.debug("s1:"+s1);
console.debug("s2:"+s2);
var s3=str.substr(-5,3);//Wor
console.debug("s3:"+s3);
var s4=str.substr(25,3);//""
console.debug("s4:"+s4);
console.debug("--------");
s1=str.substring(5);//World
s2=str.substring(5,8);//Wor
console.debug("s1:"+s1);
console.debug("s2:"+s2);
s3=str.substring(5,2);//自动交换:llo
console.debug("s3:"+s3);
s4=str.substring(5,35);//
console.debug("s4:"+s4);
console.debug("--------");
s1=str.slice(5);//World
s2=str.slice(5,8);//Wor
console.debug("s1:"+s1);
console.debug("s2:"+s2);
s3=str.slice(-5,-2);//Wor
console.debug("s3:"+s3);
s4=str.slice(-5,-8);//无法自动交换
console.debug("s4:"+s4);

后台运行结果:

[img]http://dl2.iteye.com/upload/attachment/0118/0627/30e284e2-cbd2-335d-b605-23efeba05478.png[/img]
[size=large]3.4分割方法[/size]
[size=large][color=orange]split(separator,howmany)[/color][/size]
separator 必需,用字符串或者正则表达式,从该参数指定的地方分割字符串
howmany 可选,可指定返回的数组的最大长度,如没有设置该参数,则整个字符串都会被分割,而不考虑其长度
	var str="tom,mike,jerry,andy";
var arr=str.split(",");
console.debug("str:"+str);
console.debug("arr.length:"+arr.length);
console.debug(arr);
str=",b,c,d,";
arr=str.split(",");
console.debug(arr);
console.debug("arr.length:"+arr.length);

后台运行结果:

[img]http://dl2.iteye.com/upload/attachment/0118/0629/ebabe7e9-f7f6-3201-9ec5-c0397b8ca34b.png[/img]
[size=large]3.5静态方法[/size]
[size=large][color=orange]fromCharCode(num1,num2,num3..numX)[/color][/size]
从字符编码创建一个字符串;num*必需,一个或多个Unicode值
不使用对象访问,而是使用类型名称访问的方式
可看作charCodeAt()的逆方法
var s1=String.fromCharCode(97,98,99,100);
var s2=String.fromCharCode(0x4e00,0x4e01,0x9fa5);
console.debug(s1);//abcd
console.debug(s2);//一丁X

后台运行结果:

[img]http://dl2.iteye.com/upload/attachment/0118/0631/8664c91e-e94e-3b35-8874-32d7745d0111.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值