一、字符串操作
1、字符串创建
var s1="hello"
var s2=new String("hello")
2、属性
s1.length 获取字符串长度
3、方法
(1)获取字符
charAt():返回单个给定位置的字符
charCodeAt():返回字符编码
[] var s1="hello"
(2)Ascii码
a-97
z-122
A-65
Z-90
0-48
9-57
空格-32
回车-13
二、拼接字符串
(1)concat:用于将多个字符串拼接起来,并返回拼接后的新字符串
var s1="hello"
var s2=s1.concat("world","student","1")
console.log(s2)
(2)子字符串
substr(起始位置,长度):从起始位置截取指定长度的子字符串
起始位置为负数则倒数。
console.log(s1.substr(3))
console.log(s1.substr(3,6))
console.log(s1.substr(0))
console.log(s1.substr(-3))
console.log(s1.substr(3,-4))
console.log(s1.substr(8,3))
substring(起始位置,终止位置):起始包含,终止不包含
遇到负数,自动转化为0
console.log(s1.substring(3))
console.log(s1.substring(3,7))
console.log(s1.substring(0))
console.log(s1.substring(-3))
console.log(s1.substring(3,-4))
console.log(s1.substring(8,3))
slice(起始位置,终止位置):识别负数
console.log(s1.slice(3))
console.log(s1.slice(3,6))
console.log(s1.slice(0))
console.log(s1.slice(-3))
console.log(s1.slice(3,-4))
console.log(s1.slice(8,3))
console.log(s1.slice(3,7,1))
(3)字符串大小转换
s1="hello"
s1.toLowerCase() 小写
s1.toUpperCase() 大写
(4)去空格
s1=" he llo "
s1.trim() //去左右空格
(5)split 字符串分割
s1="he,l,lo,wo,r,ld"
s1.split(",")
s1.split(",",2) 只分割前两个
(6)字符串位置方法
indexOf():返回指定字符串的第一个位置,如找不到,返回-1
第二个参数表示哈找的起始位置
lastIndexOf():从后向前查找
(7)替换replace
s1="hello"
s1.replace("e","m")