valueOf()
返回对象表示的基本字符串
toLocalString()
返回对象表示的基本字符串
toString()
返回对象表示的基本字符串
charAt()
var string = "hello"var result = string.charAt(1) // "e"// 实际应用中更多使用var newResult = sting[1] // "e"复制代码
charCodeAt()
var string = "hello"var result = string.charCodeAt(1) // 101复制代码
concat()
实际应用中更多使用 "+" 拼接字符串
var string1 = "hello"var newString = string1.concat(' world')// string1 : "hello"// newString : "hello world"var string2 = string1.concat(' world','!')// string1 : "hello"// string2 : "hello world!"复制代码
slice()
// 一个参数:返回参数位置到字符串末尾var string1 = 'hello'var res = string1.slice(1) // string1 : "hello"// res : "ello"复制代码
// 两个参数[a,b)var string1 = 'hello'var res = string1.slice(1,3)// string1 : "hello"// res : "el"复制代码
substring()
// 一个参数:返回参数位置到字符串末尾var string1 = 'hello'var res = string1.substring(1)//string1 : 'hello'//res : 'ello'复制代码
// 两个参数[a,b)var string1 = 'hello'var res = string1.substring(1,3)// string1 : "hello"// res : "el"复制代码
substr()
var string1 = 'hello'var res = string1.substr(1)// string1 : "hello"// res : "ello"复制代码
// 两个参数[a,b]var string1 = 'hello'var res = string1.substr(1,3)// string1 : "hello"// res : "ell"复制代码
indexOf()
var string = "hello"var index1 = string.indexOf('he') // 0var index2 = string.indexOf('l') // 2var index3 = string.indexOf('l',2) // 2var index4 = string.indexOf('l',3) // 3复制代码
lastIndexOf()
使用方法同indexOf,只是从字符串末尾开始查询
trim()
去除字符串开头和结尾的空格
var string = ' hello world ! 'var newString = string.trim() // "hello world !"复制代码
toLowerCase()/toLocalLowerCase()
将字符串转化成小写字母
toUpperCase()/toLocaleUpperCase()
将字符串转化成大写字母
match(reg)
参数接收一个正则表达式,返回一个数组
var string = 'cat sat dat'var res = string.match(/.at/) //res : ["cat", index: 0, input: "cat sat dat", groups: undefined]var result = string.match(/.at/g) // result : ["cat", "sat", "dat"]复制代码
search(reg)
参数接收一个正则表达式,返回第一个匹配的索引
var string = 'cat sat dat'var res = string.search(/.at/) //res : 0复制代码
replace()
(sting,string)
var text = 'cat dat sat'var result = text.replace('at','ond')// text : "cat dat sat"// result : "cond dat sat"复制代码
(reg,string)
var text = 'cat dat sat'var result = text.replace(/at/g,'ond')// text : "cat dat sat"// result : "cond dond sond"复制代码
split()
var colorText = 'red,blue,green,yellow' var color1 = colorText.split(',') //["red", "blue", "green", "yellow"] var color1 = colorText.split(',',2) //["red", "blue"]复制代码
总结
以上字符串方法都不会改变原字符串,都是返回一个字符串的副本。