成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

javascript中字符串(string)的常用方法匯總

yzd / 2948人閱讀

摘要:方法從一個字符串中返回指定的字符。查找字符串下標(biāo)并返回值序方法返回一個編碼點值的非負(fù)整數(shù)。填充從當(dāng)前字符串的開始左側(cè)應(yīng)用的。

學(xué)習(xí)javascript的過程中,總是容易string忘記方法,把字符串的一些方法全部匯總在一起,方便查看和增加記憶.

創(chuàng)建字符串
let str="hello word"  字符串
數(shù)字轉(zhuǎn)轉(zhuǎn)字符串的方法:
let  number=0;  //數(shù)字類型
console.log(String(number))  //0  字符串
console.log(new String(number))  //[String: "0"]  對象形式
靜態(tài) String.fromCharCode() 方法返回使用指定的Unicode值序 列創(chuàng)建的字符串。
console.log(String.fromCharCode(65,66,67))  //ABC 
String.fromCodePoint() 靜態(tài)方法返回使用指定的代碼點序列創(chuàng)建的字符串。
String.fromCodePoint(65, 90);   // "AZ"
charAt() 方法從一個字符串中返回指定的字符。
console.log("hello".charAt(3))  //l 
charCodeAt() 查找字符串下標(biāo)并返回unicode 值序
console.log("ABC".charCodeAt(0)) // returns 65 
codePointAt() 方法返回 一個 Unicode 編碼點值的非負(fù)整數(shù)。
console.log("ABC".codePointAt(0)) // returns 65 
concat()方法將一個或多個字符串與原字符串連接合并,形成一個新的字符串并返回。
let hello = "hello";
console.log(hello.concat(" word","!")); //hello word!
endsWith()判斷字符串結(jié)尾是否以指定的字符串結(jié)尾
endsWith(searchString,position)
searchString 為制定的字符串
position 搜索截止的下標(biāo),沒有填寫即為字符串length
let str = "To be, or not to be, that is the question.";
console.log( str.endsWith("question.") );  // true
console.log( str.endsWith("to be") );      // false
includes() 方法用于判斷一個字符串是否包含在另一個字符串中,根據(jù)情況返回true或false。
console.log("Blue Whale".includes("blue"));  //false 區(qū)分大小寫
console.log("Blue Whale".includes("Blue"));  //true 
indexOf(searchValue,fromIndex) //在字符串中查找searchValue第一次出現(xiàn)的index,fromIndex默認(rèn)為0,開始搜索的位置
console.log("Blue Whale".indexOf("Whale", 5));   //5
console.log("Blue Whale".indexOf("Whale", 12));  //-1 
lastIndexOf(searchValue,fromIndex) 方法返回指定值在調(diào)用該方法的字符串中最后出現(xiàn)的位置,如果沒找到則返回 -1
console.log("canal".lastIndexOf("a"))  // returns 3
console.log("canal".lastIndexOf("a",7))  // returns 3
match() 當(dāng)一個字符串與一個正則表達(dá)式匹配時, match()方法檢索匹配項。
var match = "For more information, see Chapter 3.4.5.1";
var re = /see (chapter d+(.d)*)/i;
var found = match.match(re);
console.log(found);  
// [ "see Chapter 3.4.5.1",
// "Chapter 3.4.5.1",
// ".1",
// index: 22,
// input: "For more information, see Chapter 3.4.5.1" ]
es6 padEnd() 方法會用一個字符串填充當(dāng)前字符串(如果需要的話則重復(fù)填充),返回填充后達(dá)到指定長度的字符串。從當(dāng)前字符串的末尾(右側(cè))開始填充。
console.log("abc".padEnd(10));          // "abc       " 長度為10
"abc".padEnd(10, "foo");   // "abcfoofoof"  //長度為10
"abc".padEnd(6, "123456"); // "abc123" 長度為6
es6padStart() 方法用另一個字符串填充當(dāng)前字符串(重復(fù),如果需要的話),以便產(chǎn)生的字符串達(dá)到給定的長度。填充從當(dāng)前字符串的開始(左側(cè))應(yīng)用的。
"abc".padStart(10);         // "       abc"  長度為10
"abc".padStart(10, "foo");  // "foofoofabc"
"abc".padStart(6,"123465"); // "123abc"
repeat()構(gòu)建并返回一個新字符串,
console.log("abcd".repeat(2));   //abcdabcd
console.log("abcd".repeat(0)); //""
console.log("abcd".repeat(3.5)); //abcdabcdabcd 小數(shù)會進行一個求余轉(zhuǎn)整數(shù)
replace() 匹配元素替換
console.log("hi word".replace("hi","hello"))  //hello word
search() 方法執(zhí)行正則表達(dá)式和 String對象之間的一個搜索匹配。
console.log("abc".search("b"))  //下標(biāo)為1
slice(beginSlice,endSlice) 方法提取一個字符串的一部分,并返回新的字符串
console.log("abc".slice(1,3))   //bc
split();把字符串根據(jù)符號改為數(shù)組
console.log("hello word".split(""));[ "h", "e", "l", "l", "o", " ", "w", "o", "r", "d" ]
es6 startsWith(searchString,position) 判斷字符串開始是否以指定字符串
searchString 指定字符串
position 開始的位置  默認(rèn)為0
let sWith="To be, or not to be, that is the question.";
console.log(sWith.startsWith("To")) //true
console.log(sWith.startsWith("to")) //false
substr() 方法返回一個字符串中從指定位置開始到指定字符數(shù)的字符。
console.log("hello word".substr(1,2))  //el
substring() 方法返回一個字符串在開始索引到結(jié)束索引之間的一個子集, 或從開始索引直到字符串的末尾的一個子集。
var anyString = "Mozilla";
console.log(anyString.substring(0,3)); //Moz
console.log(anyString.substring(3,0)); //Moz
toLocaleLowerCase() 字符串轉(zhuǎn)換為小寫
console.log("ALPHABET".toLocaleLowerCase());  //alphabet
toLocaleUpperCase() 字符串轉(zhuǎn)換為大小寫
console.log("alphabet".toLocaleUpperCase()); //ALPHABET
toLowerCase() 轉(zhuǎn)換為小寫
console.log("ALPHABET".toLowerCase());  //alphabet
toUpperCase()轉(zhuǎn)換為大寫
console.log("alphabet".toUpperCase()) //ALPHABET
trim()去除字符串兩邊的空格
console.log(" hello ".trim()); //hello 
valueOf() 返回一個string對象 的原始值
let string=new String("hello word");
console.log(string); //[String: "hello word"]
console.log(string.valueOf())  //hello word
raw() 是一個模板字符串的標(biāo)簽函數(shù)
let name="xiaozhang";
console.log(String.raw`hello ${name}`); //hello xiaozhang
常用的轉(zhuǎn)義符號