字符串相关方法
length
此属性返回字符串的长度。
语法:string.length
var txt = "Hello World!"
console.log(txt.length) // 12
search() 和 indexOf() 的区别:
方法 | 参数 | 区别 |
---|---|---|
search | searchValue,必须。查找的字符串或者正则表达式。 | 无法设置第二个开始位置参数 |
indexOf | searchValue,必需。规定需检索的字符串值。start,可选的整数参数。规定在字符串中开始检索的位置。如省略该参数,则将从字符串的首字符开始检索。 | 无法设置更强大的搜索值(正则表达式) |
trim
删除字符串的头尾空格。不会改变原始字符串。
var str = " Apple "
var newStr = str.trim()
console.log(newStr) // Apple
concat
连接两个或多个字符串
var str1 = "Hello"
var str2 = "World"
var str3 = str1.concat(" ",str2)
console.log(str3) // Hello World
// 可用于代替加运算符。例如下面两行是等效的:
var text = "Hello" + " " + "World!"
var text = "Hello".concat(" ","World!")
includes
判断字符串是否包含指定的子字符串。如果找到匹配字符串则返回 true,否则返回 false。
参数
- searchvalue 必需,要查找的字符串。
- start 可选,设置从那个位置开始查找,默认为 0。
var str = "Hello world, welcome to the Runoob。"
// 会区分大小写
console.log(str.includes("world")) // true
console.log(str.includes("World")) // false
// 从下标为7的位置开始查找
console.log(str.includes('world', 12)) // false
search
搜索特定值的字符串,并返回匹配的位置(索引)
参数:searchValue,查找的字符串或者正则表达式
var str = "The full name of China is the People's Republic of China."
var pos = str.search("China")
console.log(pos) // 17
indexOf
返回字符串中指定文本首次出现的索引(位置)
参数:
- searchValue:必需。规定需检索的字符串值。
- start:可选(整数参数)。规定在字符串中开始检索的位置。如省略该参数,则从字符串首字符开始检索。
var str = "The full name of China is the People's Republic of China."
var pos = str.indexOf("China")
console.log(pos) // 17
// indexOf() 与 search(),这两种方法是大致相等的。
// 区别在于:
// search() 方法无法设置第二个开始位置参数。
// indexOf() 方法无法设置更强大的搜索值(正则表达式)。
lastIndexOf()
返回指定文本在字符串中最后一次出现的索引(位置)
参数:
- searchValue:必需。规定需检索的字符串值。
- start:可选(整数参数)。规定在字符串中开始检索的位置。如省略该参数,则从字符串的最后一个字符处开始检索。
var str = "The full name of China is the People's Republic of China."
var pos = str.lastIndexOf("China")
console.log(pos) // 51
// 注:如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
console.log(pos) // -1
提取部分字符串 有三种提取部分字符串的方法:
方法 | 参数 | 区别 |
---|---|---|
slice | start(必需), end(可选,可为负值) | |
substring | start(必需), end(可选) | 无法接受负的索引 |
substr | start(必需), end(可选) | 第二个参数规定被提取部分的长度 |
slice
提取字符串的某个部分,并在新字符串中返回被提取的部分。
参数:
- start,起始索引(开始位置)
- end,终止索引(结束位置)
var str = "HelloWorld"
var res = str.slice(2,5)
console.log(res) // llo
// 如果某个参数为负,则从字符串的结尾开始计数。 注:负值不适用IE8以下
var res = str.slice(-5)
console.log(res) // World
var res = str.slice(-5,-2)
console.log(res) // Wor
// 如果省略第二个参数,则该方法将裁剪字符串的剩余部分:
var res = str.slice(5)
console.log(res) // World
var res = str.slice(-6)
console.log(res) // oWorld
substring
提取字符串的某个部分并在新字符串中返回被提取的部分。
substring() 类似于 slice()。不同之处在于 substring() 无法接受负的索引。
var str = "HelloWorld"
var res = str.substring(2,5)
console.log(res) // llo
// 如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。
var res = str.substring(5)
console.log(res) // World
subStr
提取字符串的某个部分并在新字符串中返回被提取的部分。
substr() 类似于 slice()。不同之处在于 substr() 第二个参数规定被提取部分的长度。
var str = "HelloWorld"
var res = str.substr(5,3)
console.log(res) // Wor
// 如果首个参数为负,则从字符串的结尾开始计算位置。第二个参数不能为负,因为它定义的是长度。
var res = str.substr(-5)
console.log(res) // World
// 如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。
var res = str.substr(5)
console.log(res) // World
replace
用另一个值替换在字符串中指定的值,不会改变调用它的字符串。返新字符串。
参数 | 描述 |
---|---|
searchvalue | 必须。规定子字符串或要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。 |
newvalue | 必需。一个字符串值。规定了替换文本或生成替换文本的函数。 |
- 文本替换
默认替换首个匹配,默认区分大小写
str = "Please visit Microsoft!"
var n = str.replace("Microsoft", "W3School")
console.log(str) // Please visit Microsoft!
// 默认只替换首个匹配
str = "Please visit Microsoft and Microsoft!"
var n = str.replace("Microsoft", "W3School")
console.log(n) // Please visit W3School and Microsoft!
// 对大小写敏感。因此不对匹配 MICROSOFT
str = "Please visit Microsoft!"
var n = str.replace("MICROSOFT", "W3School") // 此处无效
console.log(n) // Please visit Microsoft!
- 正则替换
请注意正则表达式不带引号,默认区分大小写,如需全局替换需使用 /g,如忽略大小写需使用 /i
var str="Mr Blue has a blue house and a blue car"
// 正则:全局替换,请使用正则表达式的 /g 标志(用于全局搜索)
var n=str.replace(/blue/g,"red")
console.log(n) // Mr Blue has a red house and a red car
// 正则:全局替换,并且忽略大小写
var n=str.replace(/blue/gi, "red")
console.log(n) // Mr red has a red house and a red car
split
将字符串转换为数组
参数:
- separator 可选。字符串或正则表达式,从该参数指定的地方分割字符串。
- limit 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
var str ="How are you?"
var n = str.split() // 不传参
console.log(n) // ["How are you?"]
var n =str.split(" ") // 用空格分隔
console.log(n) // ["How", "are", "you?"]
var n = str.split("") // 用空值分割
console.log(n) // ["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", "?"]
var n =str.split(" ",2)// 用空格分隔,已设置指定分割长度
console.log(n) // ["How", "are"]
match
可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
参数:regexp,必需。规定要匹配的模式的 RegExp 对象。
返回值:存放匹配结果的数组。如果没找到匹配结果返回 null 。
语法:string.match(regexp)
var str="The rain in SPAIN stays mainly in the plain"
console.log(str.match(/ain/g)) // ["ain", "ain", "ain"]
// 全局查找字符串 "ain",且不区分大小写
var str="The rain in SPAIN stays mainly in the plain"
console.log(str.match(/ain/gi)) // ["ain", "AIN", "ain", "ain"]
charAt
返回字符串中指定下标(位置)的字符串
var str = "HELLO WORLD"
console.log(str.charAt(0)) // H
charCodeAt
返回字符串中指定索引的字符 unicode 编码
var str = "HELLO WORLD"
console.log(str.charCodeAt(0)) // 72
fromCharCode
可接受一个或多个指定的 Unicode 值,然后返回一个字符串。
返回值:代表 Unicode 编码的字符。
语法:String.fromCharCode(n1, n2, …, nX)
var n = String.fromCharCode(72,69,76,76,79)
console.log(n) // HELLO
toUpperCase
用于把字符串转换为大写。
var str="orange"
console.log(str.toUpperCase()) // ORANGE
toLowerCase
用于把字符串转换为小写。
var str="ORANGE"
console.log(str.toLowerCase()) // orange
startsWith()
用于检测字符串是否以指定的子字符串开始。如果是返回 true,否则 false。对大小写敏感。
var str = "Hello world, welcome to the Runoob."
console.log(str.startsWith("Hello")) // true
console.log(str.startsWith("hello")) // false
repeat
字符串复制指定次数
参数:count,必需,设置要复制的次数。
语法:string.repeat(count)
var str = "Apple"
console.log(str.repeat(3)) // AppleAppleApple