一、操作方法
我們也可將字符串常用的操作方法歸納為增、刪、改、查,需要知道字符串的特點(diǎn)是一旦創(chuàng)建了,就不可變
增
這里增的意思并不是說(shuō)直接增添內(nèi)容,而是創(chuàng)建字符串的一個(gè)副本,再進(jìn)行操作
除了常用+
以及${}
進(jìn)行字符串拼接之外,還可通過(guò)concat
concat
用于將一個(gè)或多個(gè)字符串拼接成一個(gè)新字符串
let stringValue = "hello ";
let result = stringValue.concat("world");
console.log(result); // "hello world"
console.log(stringValue); // "hello"
刪
這里的刪的意思并不是說(shuō)刪除原字符串的內(nèi)容,而是創(chuàng)建字符串的一個(gè)副本,再進(jìn)行操作
常見(jiàn)的有:
- slice()
- substr()
- substring()
這三個(gè)方法都返回調(diào)用它們的字符串的一個(gè)子字符串,而且都接收一或兩個(gè)參數(shù)。
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7)); // "lo worl"
改
這里改的意思也不是改變?cè)址?,而是?chuàng)建字符串的一個(gè)副本,再進(jìn)行操作
常見(jiàn)的有:
-
trim()、trimLeft()、trimRight()
-
repeat()
-
padStart()、padEnd()
-
toLowerCase()、 toUpperCase()
trim()、trimLeft()、trimRight()
刪除前、后或前后所有空格符,再返回新的字符串
let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"
repeat()
接收一個(gè)整數(shù)參數(shù),表示要將字符串復(fù)制多少次,然后返回拼接所有副本后的結(jié)果
let stringValue = "na ";
let copyResult = stringValue.repeat(2) // na na
padEnd()
復(fù)制字符串,如果小于指定長(zhǎng)度,則在相應(yīng)一邊填充字符,直至滿足長(zhǎng)度條件
let stringValue = "foo";
console.log(stringValue.padStart(6)); // " foo"
console.log(stringValue.padStart(9, ".")); // "......foo"
toLowerCase()、 toUpperCase()
大小寫(xiě)轉(zhuǎn)化
let stringValue = "hello world";
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLowerCase()); // "hello world"
查
除了通過(guò)索引的方式獲取字符串的值,還可通過(guò):
-
chatAt()
-
indexOf()
-
startWith()
-
includes()
charAt()
返回給定索引位置的字符,由傳給方法的整數(shù)參數(shù)指定
let message = "abcde";
console.log(message.charAt(2)); // "c"
indexOf()
從字符串開(kāi)頭去搜索傳入的字符串,并返回位置(如果沒(méi)找到,則返回 -1 )
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
startWith()、includes()
從字符串中搜索傳入的字符串,并返回一個(gè)表示是否包含的布爾值
let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("bar")); // false
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false
二、轉(zhuǎn)換方法
split
把字符串按照指定的分割符,拆分成數(shù)組中的每一項(xiàng)
let str = "12+23+34"
let arr = str.split("+") // [12,23,34]
三、模板匹配方法
針對(duì)正則表達(dá)式,字符串設(shè)計(jì)了幾個(gè)方法:
- match()
- search()
- replace()
match()
接收一個(gè)參數(shù),可以是一個(gè)正則表達(dá)式字符串,也可以是一個(gè)RegExp
對(duì)象,返回?cái)?shù)組
let text = "cat, bat, sat, fat";
let pattern = /.at/;
let matches = text.match(pattern);
console.log(matches[0]); // "cat"
search()
接收一個(gè)參數(shù),可以是一個(gè)正則表達(dá)式字符串,也可以是一個(gè)RegExp
對(duì)象,找到則返回匹配索引,否則返回 -1
let text = "cat, bat, sat, fat";
let pos = text.search(/at/);
console.log(pos); // 1
replace()
接收兩個(gè)參數(shù),第一個(gè)參數(shù)為匹配的內(nèi)容,第二個(gè)參數(shù)為替換的元素(可用函數(shù))文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-817334.html
let text = "cat, bat, sat, fat";
let result = text.replace("at", "ond");
console.log(result); // "cond, bat, sat, fat"
參考文獻(xiàn):?
面試官:JavaScript字符串的常用方法有哪些? | web前端面試 - 面試官系列 (vue3js.cn)?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-817334.html
到了這里,關(guān)于高級(jí)編程,JavaScript筆記-字符串的常用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!