字符串方法合集

1、字符获取与查找

获取字符串长度

const str = "hello",
str.length

charAt():返回指定位置的字符串。eg:"hello.charAt(1)"返回"e"

charCodeAt():返回指定位置的字符的unicode代码点。eg:"A".charCodeAt(0)返回65

indexOf():返回指定字符串在原字符串中首次出现的位置,如果不存在则返回-1。eg:"hello world".indexOf("world")返回6

lastIndexOf():返回指定字符串在原字符串中最后一次出现的位置,如果不存在则返回-1。eg:"hello world world".lastIndexOf("world")返回12

2、字符串操作

concat():用于连接两个或多个字符串。eg:"hello".concat("world")返回”hello world“

slice():提取字符串的一部分,并返回一个新字符串。eg:"hello world".slice(0,5)返回”hello“

substring():提取字符串在指定位置之间的字符。eg:"hello world".substring(6,11)返回"world"

substr():从指定位置开始提取指定长度的字符串。eg:"hello world".substr(6,5)返回”world“

replace():用于在字符串中用一些字符替换另一些字符。eg:"hello world".replace("world","universe")返回”hello universe“

split():将字符串分割成数组。eg:"hello,world".split(",")返回["hello","world"]

3、字符串转换

toLowerCase():将字符串转换为小写。eg:"HELLO".toLowerCase()返回”hello“

toUpperCase():将字符串转换为大写。eg:"hello".toUpperCase()返回”HELLO“

trim():删除字符串两端的空白字符。eg:" hello world ".trim() 返回”hello world“

trimStart():trimStart() 方法的的行为与trim()一致,不过会返回一个从原始字符串的开头删除了空白的新字符串,不会修改原始字符串

const s = '  abc  ';
s.trimStart()   // "abc  "

trimEnd():trimEnd() 方法的的行为与trim()一致,不过会返回一个从原始字符串的结尾删除了空白的新字符串,不会修改原始字符串

const s = '  abc  ';
s.trimEnd()   // "  abc"

4、获取字符串本身

valueOf():返回某个字符串对象的原始值,该方法通常由 JavaScript 自动进行调用

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.valueOf(); //Banana,Orange,Apple,Mango

toString():返回字符串对象本身,将其他类型数据转换成字符串

let str = "abcdef",str2 =123123123
console.log(str.toString()) // "abcdef"
console.log(typeof(str2.toString())) //string

5、补齐字符串长度

padStart():用于头部补全。该方法有两个参数,其中第一个参数是一个数字,表示字符串补齐之后的长度;第二个参数是用来补全的字符串

'x'.padStart(1, 'ab') // 'x'
'x'.padStart(5, 'ab') // 'ababx'
'x'.padStart(4, 'ab') // 'abax'

'x'.padStart(4) // '   x'

"1".padStart(3, '0')   // 输出结果: '001'
"15".padStart(3, '0')  // 输出结果: '015'

padEnd():用于尾部补全。该方法也是接收两个参数,第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串

'x'.padEnd(5, 'ab') // 'xabab'
'x'.padEnd(4, 'ab') // 'xaba'

6、字符串转数字

parseInt():

parseInt(string, radix)

string:必需。要被解析的字符串。

radix:可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间

parseFloat():可解析一个字符串,并返回一个浮点数

parseFloat("10.00")      // 输出结果:10.00
parseFloat("10.01")      // 输出结果:10.01
parseFloat("-10.01")     // 输出结果:-10.01
parseFloat("40.5 years") // 输出结果:40.5

如果参数字符串的第一个字符不能被解析成为数字,则 parseFloat 返回 NaN

parseFloat("new40.5")    // 输出结果:NaN

7、其它方法

includes():判断字符串是否包含指定的子字符串,返回true或false。eg:"hello world".includes("world")返回true

startsWith():判断字符串是否以指定的子字符串开头,返回true或false。eg:"hello world".startsWith("hello")返回true

endsWith():判断字符串是否以指定子字符串结尾,返回true或false。eg:"hello world".endWith("world")返回true

repeat():返回一个新字符串,由指定次数重复原字符串组成。eg:"hello".repeat(3)返回”hellohellohello“。如果参数是小数,会向下取整。如果参数是负数或者Infinity,会报错。如果参数是 0 到-1 之间的小数,则等同于 0,这是因为会先进行取整运算。0 到-1 之间的小数,取整以后等于-0,repeat视同为 0。如果参数是NaN,就等同于 0。如果repeat的参数是字符串,则会先转换成数字。

match():该方法用于在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置

let str = "abcdef";
console.log(str.match("c")) // ["c", index: 2, input: "abcdef", groups: undefined]

search():方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串

let str = "abcdef";
str.search(/bcd/)   // 输出结果:1

//////////////方法的实际应用场景,比较常用的方法,注意事项......

 

posted @ 2025-04-19 00:13  k954  阅读(27)  评论(0)    收藏  举报