JS字符串常用方法
所有的字符串方法返回的都是新字符串,不会改变原字符串
1、length 返回字符串长度
var str = "adasd"
console.log(str.length) //5
2、indexOf() 返回字符串中指定文本首次出现的索引,未出现返回-1;可接收两个参数,第一个参数是查找的内容,第二个参数可传可不传,从指定位置开始
var str = "akfsqwekfs"
var a = str.indexOf('a')
console.log(a) //0
var b = str.indexOf('f',2) //从下标2开始检索
console.log(b) //2
var c = str.indexOf('A')
console.log(c) //-1
3、lastIndexOf() 返回字符串中指定文本最后一次出现的位置
var str = "dfiefnds"
var a = str.lastIndexOf('d')
console.log(a) //6
var b = str.lastIndexOf('v')
console.log(b) //-1
4、search() 搜索特定值的字符串或检索与正则表达式相匹配的子字符串,并返回匹配的位置
var str = "123dfdsfsfcr4d"
var a = str.search("4")
console.log(a) //12
var str = "Hello World!"
console.log(str.search(/World/)) //6
var str = "Hello World!"
console.log(str.search(/world/)) //-1 search()对大小写敏感
var str = "Hello World!"
console.log(str.search(/world/i)) //6 忽略大小写进行检索
slice/substring/substr异同点
相同点:三个方法都对字符串进行截取,返回一个新字符串
不同点:参数不同
- substring(start,end) 提取字符串中介于两个下标之间的字符。包括start,不包括end
- substr(start,length) 提取字符串中从start下标开始指定数目的字符
- slice(start,end) 提取字符串的某个部分,以新的字符返回被提取的部分。包括start,不包括end
5、slice() 提取字符串的某个部分并在新字符串中返回被提取的部分(两个参数:第一个参数是截取字符串的起始位置,第二个是终止位置,截取的字符串包括第一个参数但不包括最后一个参数;如果只传一个参数,截取从参数开始之后的所有字符串)
var str = "Hello World!"
console.log(str.slice(6)) //World!
console.log(str.slice(3,8)) //lo Wo
//当参数为负数时,从右往左数,从一开始
console.log(str.slice(1,-1)) //ello World
//start到end的顺序始终是从左到右,如果最终start的位置在end的右边,则返回""
console.log(str.slice(2,1)) //
//start所在位置是6,end位置是1,starts位置在end右边,返回""
console.log(str.slice(-5,1)) //
//start所在位置是6,end位置是7,starts位置在end左边
console.log(str.slice(-6,-5)) //W
//start跟end位置相同,则返回空字符串
console.log(str.slice(-6,-6)) //
6、substring() 提取字符串中介于两个下标之间的字符。包括start,不包括end
var str = "0123456789"
console.log(str.substring(2,5)) //234
//如果start跟end相等,则返回一个空字符串
console.log(str.substring(2,2)) //
//如果start大于end,那么提取字符串之前substring会交换这两个参数
console.log(str.substring(5,1)) //1234 -->相当于str.substring(1,5)
//如果start、end有负数,会自动把负数转成0,继续上述规则
console.log(str.substring(2,-1)) //01 -->相当于str.substring(0,2)
//如果start和end有正小数,那么小数会向下取整,继续上述规则
console.log(str.substring(2,5.6)) //234
//如果start和end有负小数,那么负数转成0,继续上述规则
console.log(str.substring(2,-6.4)) //01
//相当于str.substring(2,0)-->str.substring(0,2)
//如果start和end有字符串,那么会先parseInt(),如果转换结果为NAN,那么就转为0,继续上述规则
console.log(str.substring(5,'2.5')) //234
//相当于str.substring(5,2)-->str.substring(2,5)
console.log(str.substring(5,'ss')) //01234
//相当于str.substring(5,NaN)-->str.substring(5,0)-->str.substring(0,5)
7、substr()在字符串中抽取从start下标开始的指定数目的字符串
var str = "0123456789"
console.log(str.substr(2,5)) //23456
console.log(str.substr(2,15)) //23456789 -->最多截取到字符串末端
//如果length为负值,那么会直接当成0处理,最终返回""
console.log(str.substr(4,-2)) //""
//如果start或length为小数,那么会截取小数部分
console.log(str.substr(1.2,5.2)) //12345
console.log(str.substr(1.8,5.8)) //12345
//如果start和length有字符串,那么会先进行parseInt(),如果结果转换为NaN,那么久转换为0,继续上述规则
console.log(str.substr('aa',5)) //01234
//str.substr(NaN,5) --> str.substr(0,5)
console.log(str.substr(5,'ss')) //""
//str.substr(5,NaN) --> str.substr(5,0)
8、toUpperCase() 字符串转为大写
var str = "hello world"
console.log(str.toUpperCase()) //HELLO WORLD
9、toLowerCase() 字符串转为小写
var str = "HELLO world"
console.log(str.toLowerCase()) //hello world
10、concat() 常用于连接两个字符串,返回一个新字符串,不改变原字符串
var str1 = "abc";
var str2 = "def";
console.log(str1.concat(str2)) //abcdef
console.log(str1) //abc
console.log(str2) //def
//concat()可以接受多个参数
console.log(str1.concat('d','e')) //abcde
//如果参数不是字符串,concat()方法会将其先转为字符串,然后再连接
var one = 1;
var two = 2;
var three = 3;
console.log("".concat(one,two,three)) //123
console.log(one+two+three) //6
11、trim() 删除字符串两端的空白符,并返回处理后的字符串
var str = " str str "
console.log(str.trim()) //str str
//单独去除左侧空格trimLeft()
console.log(str.trimLeft()) //str str
//单独去除右侧空格trimRight()
console.log(str.trimRight()) // str str
12、replace() 替换字符串
repalce()方法可以接受两个参数:第一个参数可以是RegExp对象或者是一个字符串,第二个参数可以是一个字符串或者是一个函数。如果第一个参数是字符串,那么只会替换第一个字符串。如果想替换所有的字符串,必须使用正则表达式。
var str = "hello world"
var newStr = str.replace("o","h")
console.log(newStr) //hellh world
var str = "hello world"
var newStr = str.replace(/o/g,function(match,pos,orginText) {
return "a"
})
console.log(newStr); //hella warld
replace()方法中,第一个参数是模式匹配,第二个参数是一个函数。函数有三个参数:第一个参数试试匹配到的字符串,第二个参数是匹配的位置,第三个参数是原字符串。在函数里面可以对字符串进行操作。使用函数作为第二个参数,可以做一些复杂的替换,比如当匹配多个字符的时候,可以对不同的字符做不同的替换。
var str = "hello world"
var newStr = str.replace(/[ol]/g,function(match,pos,orginText) {
if(match == "o") {
return "a"
} else {
return "b"
}
})
console.log(newStr) //hebba warbd
13、split() 把字符串分割成字符串数组
split()方法可以接收两个参数:第一个参数是字符串或正则表达式,第二个参数可指定返回的数组的最大长度。
var str = "hello world hello world"
console.log(str.split( )) //['hello world hello world']
//省略分割参数
console.log(str.split()) //['hello world hello world']
//分割每个字符,包括空格
console.log(str.split("")) //['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
//使用limit参数
console.log(str.split(" ",3)) //['hello', 'world', 'hello']
//使用一个字符作为分隔符
console.log(str.split("o")) //['hell',' w','rld hell',' w','rld']
//用特定符号分割
var str = "2:3:4:5"
console.log(str.split(":")) //['2','3','4','5']
var str = "|1|2|3|4|"
console.log(str.split("|")) //['','1','2','3','4','']
//利用正则分割多个符号
var str = "T:w-x";
console.log(str.split(/[:,-]/)) //['T', 'w', 'x']
14、charAt() 返回字符串指定下标的字符串
var str = "hello world!"
console.log(str.charAt(0)) //h
15、charCodeAt() 返回字符串中指定索引的字符unicode编码
var str = "hello world!"
console.log(str.charCodeAt(0)) //104