字符串操作(2)


1.字符串删除
(1).replace
     var s1="hello"
     s2=s1.replace("e","")
     alert(s2)
     s1[3]
(2)删除字符串join的第一位 str.substr(1);
  删除字符串的最后一位 str.substr(0,str.length-2); 
  substr
(3)截取字符串的开始和结束 s1.substring(3,s1.length-)
substring
(4)删除指定的字符
split
s1=“hellowoeld“
s2=s1.split(”o“)    //{hell,w,rld}
s2.join(”“)

2.字符串的比较
调用本地排序规则
s1=“abc”
s2=“edf”
s1.localeCompare(s2)
"yellow".localeCompare("yellow") 0
"yellow".localeCompare("blue")  1
"yellow".localeCompare("zoo")  -1

//英文比较
v1.localeCompare(v2)
v1=v2返回0
v1排在v2之前返回-1
v1排在v2之后返回1
//中文比较是按照拼音
a1=["哈尔滨","上海","杭州","深圳"]
a1.sort((a,b)=>{return a.localeCompare(b)})
console.log(a1)
(2)alert("a">"A")true 按ASCII码比较
3.字符串编码
(1)charCodeAt()
s1="a"
charCodeAt(0)//97

s1="海"
s1.charCodeAt(0)
alert(s1.charCodeAt(0))//28023
(2) fromCharCode()
根据ASCII或unicode码反向解析
s1=String.fromCharCode(65)//A
s1=String.fromCharCode(28023)//海4e00-9fcf
(3)escape()   输出unicode码
unescape()   反向输出
s1="海"
escape(s1)//\u6D77
unescape("\u6D77")//海
enCodeURI(s1) //加密
deCodeURI(s1)//解密 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值