@(JavaScript基礎(chǔ))[String.prototype]
關(guān)于字符串方法總結(jié)
String.prototype
屬性:
String.prototype.constructor
String.prototype.length
方法:
// 以下兩種方法幾乎沒什么用
----------------------------------------------------------------------------
String.prototype.toString():
描述:
返回用字符串表示的特定對象,和 String.prototype.valueOf() 方法返回值一樣
String.prototype.valueOf():
描述:
返回特定對象的原始值,該值等同于String.prototype.toString()
----------------------------------------------------------------------------
String.prototype[@@iterator]()
描述:
返回一個(gè)新的Iterator對象,它遍歷字符串的代碼點(diǎn),返回每一個(gè)代碼點(diǎn)的字符串值
例子:
let str='hello world';
let newStr=str[Symbol.iterator]();
console.log(newStr.next().value); // 'h'
console.log(newStr.next().value); // 'e'
----------------------------------------------------------------------------
字符串的方法基本上可以分為增刪改查
查找:
// 以下方法會(huì)返回該字符串是否存在某特定字符串的boolean值
----------------------------------------------------------------------------
String.prototype.includes(searchString[, position]): (ES6)
參數(shù):
searchString:
要被搜索的字符串,區(qū)分大小寫
position:
從當(dāng)前字符串的哪個(gè)索引位置開始搜索字符串,默認(rèn)值為0
返回值:
boolean值
----------------------------------------------------------------------------
String.prototype.startsWith(searchString [, position]):
描述:
判斷字符串的起始位置是否匹配其他字符串中的字符
參數(shù):
searchString:
要搜索的子字符串
position:
要搜索字符串的位置,默認(rèn)值為0
返回值:
boolean值
例子:
"hello world".startsWith("hello"); // true
"hello world".startsWith("world"); // false
"hello world".startsWith("world",6); // true
----------------------------------------------------------------------------
String.prototype.endsWith(searchString [, position]):
描述:
判斷一個(gè)字符串的結(jié)尾是否包含其他字符串中的字符
參數(shù):
searchString:
要被搜索的字符串,區(qū)分大小寫
position:
searchString 的結(jié)束位置,默認(rèn)值為 str.length
返回值:
boolean值
例子:
"abccd".endsWith("d") //true
"abccd".endsWith("cd",0) //false
"abccd".endsWith("cd",1) //false
"abccd".endsWith("cd",2) //false
"abccd".endsWith("cd",3) //false
"abccd".endsWith("cd",4) //false
"abccd".endsWith("cd",5) //false
----------------------------------------------------------------------------
// 以下方法返回特定位置的字符或字符碼
----------------------------------------------------------------------------
String.prototype.charAt(index):
參數(shù):
index
返回值:
返回特定位置的字符
----------------------------------------------------------------------------
String.prototype.charCodeAt(index):
參數(shù):
index
返回值:
返回表示給定索引的字符的 UTF-16 代碼單元值的數(shù)字,如果索引超出范圍,則返回 NaN
例子:
"ABC".charCodeAt(0) // returns 65
----------------------------------------------------------------------------
String.prototype.codePointAt(pos):
參數(shù):
pos:
這個(gè)字符串中需要轉(zhuǎn)碼的元素的位置
返回值:
返回使用UTF-16編碼的給定位置的值的非負(fù)整數(shù)。
例子:
'ABC'.codePointAt(1); // 66
'\uD800\uDC00'.codePointAt(0); // 65536
----------------------------------------------------------------------------
// 以下方法返回特定字符串所在的索引
----------------------------------------------------------------------------
String.prototype.indexOf(searchValue[, fromIndex]):
描述:
從字符串對象中返回首個(gè)被發(fā)現(xiàn)的給定值的索引值,如果沒有找到則返回-1
參數(shù):
searchString:
要被搜索的字符串,區(qū)分大小寫
fromIndex:
開始查找的位置,默認(rèn)值為0
返回值:
指定值的第一次出現(xiàn)的索引; 如果沒有找到 -1
----------------------------------------------------------------------------
String.prototype.lastIndexOf(searchValue[, fromIndex]):
描述:
從字符串對象中返回最后一個(gè)被發(fā)現(xiàn)的給定值的索引值,如果沒有找到則返回-1
參數(shù):
searchString:
要被搜索的字符串,區(qū)分大小寫
fromIndex:
開始查找的位置,默認(rèn)值為str.length
返回值:
指定值的第一次出現(xiàn)的索引; 如果沒有找到 -1
例子:
"abcredfeefe".lastIndexOf("f") // 9
"abcredfeefe".lastIndexOf("f",9) // 9
"abcredfeefe".lastIndexOf("f",8) // 6
"abcredfeefe".lastIndexOf("f",6) // 6
"abcredfeefe".lastIndexOf("f",5) // -1
----------------------------------------------------------------------------
String.prototype.search(regexp):
描述:
對正則表達(dá)式和指定字符串進(jìn)行匹配搜索,返回第一個(gè)出現(xiàn)的匹配項(xiàng)的下標(biāo)
參數(shù):
regexp:
一個(gè)正則表達(dá)式(regular expression)對象
返回值:
返回正則表達(dá)式在字符串中首次匹配項(xiàng)的索引
例子:
let reg=/ab/gi;
"wabreabr".search(reg); // 1
----------------------------------------------------------------------------
// 以下方法會(huì)返回符合查找結(jié)果的字符串組成的數(shù)組
----------------------------------------------------------------------------
String.prototype.split([separator[, limit]]):
描述:
通過分離字符串成字串,將字符串對象分割成字符串?dāng)?shù)組
參數(shù):
separator:
指定表示每個(gè)拆分應(yīng)發(fā)生的點(diǎn)的字符串。separator 可以是一個(gè)字符串或正則表達(dá)式
如果在str中省略或不出現(xiàn)分隔符,則返回的數(shù)組包含一個(gè)由整個(gè)字符串組成的元素
如果分隔符為空字符串,則將str原字符串中每個(gè)字符的數(shù)組形式返回
limit:
限定返回的分割片段數(shù)量
返回值:
由分割字符串分割出來的字符串組成的數(shù)組
例子:
"abcd".split(); // ["abcd"]
"abcd".split(); // ["a", "b", "c", "d"]
"ab ba be eb".split(" "); // ["ab", "ba", "be", "eb"]
"ab ba be eb".split("b"); // ["a", " ", "a ", "e e", ""]
"ab ba be eb".split(/b/gi); // ["a", " ", "a ", "e e", ""]
"ab ba be eb".split(/b/g); // ["a", " ", "a ", "e e", ""]
----------------------------------------------------------------------------
String.prototype.match(regexp)
描述:
如果正則表達(dá)式?jīng)]有 g 標(biāo)志,則 str.match() 會(huì)返回和 RegExp.exec() 相同的結(jié)果。
而且返回的 Array 擁有一個(gè)額外的 input 屬性,該屬性包含被解析的原始字符串。
另外,還擁有一個(gè) index 屬性,該屬性表示匹配結(jié)果在原字符串中的索引(以0開始)。
如果正則表達(dá)式包含 g 標(biāo)志,則該方法返回一個(gè) Array,它包含所有匹配的子字符串而不是匹配對象。
捕獲組不會(huì)被返回(即不返回index屬性和input屬性)。如果沒有匹配到,則返回 null
參數(shù):
regexp:
一個(gè)正則表達(dá)式對象。
如果傳入一個(gè)非正則表達(dá)式對象,則會(huì)隱式地使用 new RegExp(obj) 將其轉(zhuǎn)換為一個(gè) RegExp 。
如果你未提供任何參數(shù),直接使用 match() ,那么你會(huì)得到一個(gè)包含空字符串的 Array :[""]
返回值:
由正則匹配到的字符串組成的數(shù)組
例子:
"abcdabeabeabiiiabeeaabbabb".match(); // 無參數(shù)
// ["", index: 0, input: "abcdabeabeabiiiabeeaabbabb"]
"abcdabeabeabiiiabeeaabbabb".match("ab"); // 非RegExp對象
// ["ab", index: 0, input: "abcdabeabeabiiiabeeaabbabb"]
const rep=/ab/;
"abcdabeabeabiiiabeeaabbabb".match(rep); // 非g
// ["ab", index: 0, input: "abcdabeabeabiiiabeeaabbabb"]
"bcdabeabeabiiiabeeaabbabb".match(rep);
// ["ab", index: 3, input: "bcdabeabeabiiiabeeaabbabb"]
const repg=/ab/g;
"abcdabeabeabiiiabeeaabbabb".match(repg);
// ["ab", "ab", "ab", "ab", "ab", "ab", "ab"]
----------------------------------------------------------------------------
增加:
// 增加單個(gè)字符串值
----------------------------------------------------------------------------
String.prototype.padStart(targetLength [, padString])
描述:
在當(dāng)前字符串首部填充指定的字符串, 直到達(dá)到指定的長度。 返回一個(gè)新的字符串
參數(shù):
targetLength:
當(dāng)前字符串需要填充到的目標(biāo)長度
padString:
填充字符串
返回值:
在原字符串首部填充指定的填充字符串直到目標(biāo)長度所形成的新字符串
例子:
"abc".padStart(10); // " abc"
"abc".padStart(10,"foo"); // "foofoofabc"
"abc".padStart(10,"foobarbaz"); // "foobarbabc"
"abc".padStart(2); // "abc"
----------------------------------------------------------------------------
String.prototype.padEnd(targetLength [, padString])
描述:
在當(dāng)前字符串尾部填充指定的字符串, 直到達(dá)到指定的長度。 返回一個(gè)新的字符串
參數(shù):
targetLength:
當(dāng)前字符串需要填充到的目標(biāo)長度
padString:
填充字符串
返回值:
在原字符串末尾填充指定的填充字符串直到目標(biāo)長度所形成的新字符串
例子:
"abc".padEnd(10); // "abc "
"abc".padEnd(10,"foo"); // "abcfoofoof"
"abc".padEnd(10,"foo"); // "abcfo"
"abc".padEnd(2); // "abc"
----------------------------------------------------------------------------
String.prototype.repeat(count)
描述:
返回指定重復(fù)次數(shù)的由元素組成的字符串對象
參數(shù):
count:
介于0和正無窮大之間的整數(shù)
返回值:
包含指定字符串的指定數(shù)量副本的新字符串
例子:
"ab".repeat(0) // ""
"ab".repeat(2) // "abab"
"ab".repeat(0) // Uncaught RangeError: Invalid count value
"ab".repeat(3.5) // "ababab"
// 合并字符串
----------------------------------------------------------------------------
String.prototype.concat(string2, string3[, ..., stringN]):
參數(shù):
string:
要和原字符串拼接的字符串
返回值:
返回一個(gè)新的字符串
----------------------------------------------------------------------------
編輯:
// 以下方法轉(zhuǎn)換大小寫
----------------------------------------------------------------------------
String.prototype.toLocaleLowerCase():
描述:
根據(jù)當(dāng)前區(qū)域設(shè)置,將符串中的字符轉(zhuǎn)換成小寫。對于大多數(shù)語言來說,toLowerCase的返回值是一致的
String.prototype.toLocaleUpperCase():
描述:
根據(jù)當(dāng)前區(qū)域設(shè)置,將字符串中的字符轉(zhuǎn)換成大寫,對于大多數(shù)語言來說,toUpperCase的返回值是一致的
String.prototype.toLowerCase():
描述:
將字符串轉(zhuǎn)換成小寫并返回
String.prototype.toUpperCase():
描述:
將字符串轉(zhuǎn)換成大寫并返回
----------------------------------------------------------------------------
// 截取字符串
----------------------------------------------------------------------------
String.prototype.slice(beginSlice[, endSlice]):
描述:
摘取一個(gè)字符串區(qū)域,返回一個(gè)新的字符串。
參數(shù):
beginSlice:
endSlice:
該endSlice位置的字符無法取到
返回值:
返回一個(gè)新的字符串,不會(huì)影響原字符串
例子:
let str="hello world";
let newStr=str.slice(0,8);
console.log(str); // hello world
console.log(newStr); // hello wo
----------------------------------------------------------------------------
String.prototype.substr(start[, length]):
描述:
通過指定字符數(shù)返回在指定位置開始的字符串中的字符。
參數(shù):
start:
開始提取字符的位置
length:
提取的字符數(shù)
返回值:
返回一個(gè)字符串中從指定位置開始到指定字符數(shù)的字符
例子:
'hello world'.substr(); // 'hello world'
'hello world'.substr(3); // 'lo world'
'hello world'.substr(2,5); // 'llo w'
----------------------------------------------------------------------------
String.prototype.substring(indexStart[, indexEnd]):
描述:
返回在字符串中指定兩個(gè)下標(biāo)之間的字符
參數(shù):
indexStart:
起始位置
indexEnd:
結(jié)束位置,該位置的字符串無法取到
返回值:
返回在字符串中指定兩個(gè)下標(biāo)之間的字符
例子:
"hello world".substring(2,8); // "llo wo"
"hello world".substring(2); // "llo world"
----------------------------------------------------------------------------
總結(jié):
String.prototype.slice和String.prototype.substring方法截取特定兩個(gè)位置之間的字符串,且結(jié)束位置的字符無法取到
String.prototype.substr方法截取從某特定位置開始,長度一定的字符串
// 替換
----------------------------------------------------------------------------
String.prototype.replace(regexp|substr, newSubStr|function):
描述:
被用來在正則表達(dá)式和字符串直接比較,然后用新的子串來替換被匹配的子串
參數(shù):
regexp (pattern):
一個(gè)RegExp 對象或者其字面量
substr (pattern):
一個(gè)要被 newSubStr 替換的字符串
newSubStr (replacement):
用于替換掉第一個(gè)參數(shù)在原字符串中的匹配部分的字符串
function (replacement):
一個(gè)用來創(chuàng)建新子字符串的函數(shù),該函數(shù)的返回值將替換掉第一個(gè)參數(shù)匹配到的結(jié)果
返回值:
一個(gè)部分或全部匹配由替代模式所取代的新的字符串,不改變調(diào)用它的字符串本身
例子:
let rep=/ab/gi;
"qabwabeabrr".replace(rep,"oo"); // "qoowooeoorr"
"qabwabeabrr".replace("ab","oo"); // "qoowooeoorr"
const bar=( )=>{return "oo"}
"qabwabeabrr".replace(rep,bar); // "qoowooeoorr"
"qabwabeabrr".replace("ab",bar); // "qoowooeoorr"
----------------------------------------------------------------------------
刪除:
// 刪除起止位置處的空格
----------------------------------------------------------------------------
String.prototype.trim():
描述:
從字符串的開始和結(jié)尾去除空格
String.prototype.trimLeft():
描述:
從字符串的左側(cè)去除空格
String.prototype.trimRight():
描述:
從字符串的右側(cè)去除空格
----------------------------------------------------------------------------
以下方法中會(huì)修改原字符串,返回新字符串:
String.prototype.toUpperCase
String.prototype.toLowerCase
String.prototype.toLocaleLowerCase
String.prototype.toLocaleUpperCase
String.prototype.trim
String.prototype.trimLeft
String.prototype.trimRight
以下方法中不會(huì)修改原字符串,返回新字符串:
String.prototype.padStart
String.prototype.padEnd
String.prototype.repeat
String.prototype.concat
String.prototype.sublice
String.prototype.substr
String.prototype.substring
String.prototype.replace