创建一个字符串对象
- new String()
var txt = new String('Hello')
- 直接创建:
var txt = 'Hello'
字符串对象方法
- charAt():返回指定位置的字符。
let str = 'Hello World'
let n = str.charAt(6)
console.log(n) // W
返回字符串中最后一个字符:
let str = 'Hello World'
let n = str.charAt(str.length - 1)
console.log(n) // d
- charCodeAt():返回指定位置的字符的 Unicode 编码。
let str = 'Hello World'
let n = str.charCodeAt(0)
console.log(n) // 72
- concat():连接两个或多个字符串。不改变原来的字符串,返回一个新字符串。
let str1 = 'Hello '
let str2 = 'World'
let result = str1.concat(str2)
console.log(result) //Hello World
- endsWith():判断当前字符串是否以指定的字符串结尾(区分大小写)。返回 true 或 false。
let str = 'Hello World'
console.log(str.endsWith('world')) // false
console.log(str.endsWith('World')) // true
设置不同字符串长度来比较:
let str = 'Hello World'
console.log(str.endsWith('llo',5)) // true
- fromCharCode():可接受一个指定的 Unicode 值,然后返回一个字符串。
let n = String.fromCharCode(65)
console.log(n) // A
- indexOf():返回某个指定的字符串值在字符串中首次出现的位置。没有找到匹配的字符串则返回 -1 。
let str = "Hello world, welcome to the universe."
let n = str.indexOf('welcome')
console.log(n) //13
在字符串第五个位置开始查找字符 e 第一次出现的位置:
let str = "Hello world, welcome to the universe."
let n = str.indexOf('e',5)
console.log(n) //14
- includes():判断字符串是否包含指定的子字符串。找到匹配的字符串则返回true,否则返回false。
let str = "Hello world, welcome to the universe."
let n = str.includes('world')
console.log(n) //true
从第12个索引位置开始查找字符串:
let str = "Hello world, welcome to the universe."
let n = str.includes('world',12)
console.log(n) //false
- lastIndexOf():返回一个指定的字符串值
最后出现
的位置,如果指定第二个参数 start,则在一个字符串中的指定位置
从后向前
搜索。
注意:该方法将从后向前检索字符串,但返回是从起始位置 (0) 开始计算子字符串最后出现的位置。 看它是否含有字符串。
var str = "I am from runoob,welcome to runoob site."
var n = str.lastIndexOf("runoob")
console.log(n) //28
从第 20 个字符开始查找字符串 runoob 最后出现的位置:
var str = "I am from runoob,welcome to runoob site."
var n = str.lastIndexOf("runoob", 20)
console.log(n) //10
- match():在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
let str = "The rain in SPAIN stays mainly in the plain"
let n = str.match(/ain/g) //['ain', 'ain', 'ain']
let n = str.match(/ain/gi) //['ain', 'AIN', 'ain', 'ain']
console.log(n)
- /g:区分不区分大小写
- /gi:区分大小写
- repeat():字符串复制指定次数。
let str = 'Hello '
console.log(str.repeat(2)) //Hello Hello
- replacce():在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。该方法不改变原始字符串。
let str = 'Hello World'
let n = str.replace('World','JavaScript')
console.log(n) //Hello JavaScript
全局替换:
let str = "Mr Blue has a blue house and a blue car."
let n = str.replace(/blue/g,'red')
console.log(n) //Mr Blue has a red house and a red car.
- replaceAll():在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串,该函数会替换所有匹配到的子字符串。该方法不会改变原始字符串。
var str = "Visit Microsoft! Visit Microsoft!"
var n = str.replaceAll("Microsoft","Runoob")
console.log(n) //Visit Runoob!Visit Runoob!
- search():检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。如果没有找到任何匹配的子串,则返回 -1。
let str = "Hello new world"
let n = str.search('world')
console.log(n) //10
大小写敏感查找:
let str = "Mr. Blue has a blue house"
let n = str.search('blue')
console.log(n) //15
大小写不敏感查找:
let str = "Mr. Blue has a blue house"
let n = str.search(/blue/i)
console.log(n) //4
- slice():提取字符串的某个部分,并以新的字符串返回被提取的部分。
语法:
slice(start, end)
- start(包含)、end(不包含)
- start 参数字符串中第一个字符位置为 0, 第二个字符位置为 1, 以此类推。
- 如果是负数表示从尾部截取多少个字符串,slice(-2) 表示提取原数组中的倒数第二个元素到最后一个元素(包含最后一个元素)。
- end 参数如果为负数,-1 指字符串的最后一个字符的位置,-2 指倒数第二个字符,以此类推。
提取字符串片段:
let str = "Hello world"
let n = str.slice(1,5)
console.log(n) //ello
提取所有字符串:str.slice(0)
- split():用于把一个字符串分割成字符串数组。该方法不改变原始字符串。
let str = "Hello world Hello JS"
let n = str.split(" ")
console.log(n) //['Hello', 'world', 'Hello', 'JS']
分隔每个参数,包括空格:
let str = "Hello world"
let n = str.split("")
console.log(n) //['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
使用一个字符作为分隔符:
let str = "How are you doing today?"
let n = str.split("o")
console.log(n) //['H', 'w are y', 'u d', 'ing t', 'day?']
- startsWith():用于检测字符串是否以指定的子字符串开始。返回 true 或 false。该方法对大小写敏感。
let str = "How are you doing today?"
let n = str.startsWith("How")
console.log(n) //true
查看从第 6 个 索引位置是否以 world 开头:
let str = "Hello world."
let n = str.startsWith("world",6)
console.log(n) //true
- substr():在字符串中抽取从 开始 下标开始的指定数目的字符。该方法不会改变原字符串。
let str = "Hello world!"
let n = str.substr(2,3)
console.log(n) //llo
注:substr() 的参数指定的是子串的开始位置和长度,因此它可以替代 substring() 和 slice() 来使用。
从第二个位置中提取字符串:
let str = "Hello world!"
let n = str.substr(2)
console.log(n) //llo world!
- subString():用于提取字符串中介于两个指定下标之间的字符。返回的子串包括
开始
处的字符,但不包括结束
处的字符。
let str = "Hello world!"
let n = str.substring(2,3)
console.log(n) //l
- toLowerCase():把字符串转换为小写。
let str = "Hello world!"
let n = str.toLowerCase()
console.log(n) //hello world!
- toUpperCase():把字符串转换为大写。
let str = "Hello world!"
let n = str.toUpperCase()
console.log(n) //HELLO WORLD!
- trim():用于删除字符串的头尾空白符,空白符包括:空格、制表符 tab、换行符等其他空白符等。trim() 方法不会改变原始字符串。trim() 方法不适用于 null, undefined, Number 类型。
let str = " Hello "
let n = str.trim()
console.log(n) //Hello
- valueOf():返回 String 对象的原始值。
let str="Hello world!";
console.log(str.valueOf()) //Hello world!
- toString():返回一个表示 String 对象的值。
let str = "Runoob"
let res = str.toString()
console.log(res) //Runoob
不积跬步无以至千里 不积小流无以成江海