javascript中字符串(string)的經常使用方法彙總

學習javascript的過程當中,老是容易string忘記方法,把字符串的一些方法所有彙總在一塊兒,方便查看和增長記憶.javascript

建立字符串

let str='hello word'  字符串

數字轉轉字符串的方法:

let  number=0;  //數字類型
console.log(String(number))  //0  字符串
console.log(new String(number))  //[String: '0']  對象形式

靜態 String.fromCharCode() 方法返回使用指定的Unicode值序 列建立的字符串。

console.log(String.fromCharCode(65,66,67))  //ABC

String.fromCodePoint() 靜態方法返回使用指定的代碼點序列建立的字符串。

String.fromCodePoint(65, 90);   // "AZ"

charAt() 方法從一個字符串中返回指定的字符。

console.log('hello'.charAt(3))  //l

charCodeAt() 查找字符串下標並返回unicode 值序

console.log("ABC".charCodeAt(0)) // returns 65

codePointAt() 方法返回 一個 Unicode 編碼點值的非負整數。

console.log("ABC".codePointAt(0)) // returns 65

concat()方法將一個或多個字符串與原字符串鏈接合併,造成一個新的字符串並返回。

let hello = 'hello';
console.log(hello.concat(' word','!')); //hello word!

endsWith()判斷字符串結尾是否以指定的字符串結尾

endsWith(searchString,position)
searchString 爲制定的字符串
position 搜索截止的下標,沒有填寫即爲字符串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() 方法用於判斷一個字符串是否包含在另外一個字符串中,根據狀況返回true或false。

console.log('Blue Whale'.includes('blue'));  //false 區分大小寫
console.log('Blue Whale'.includes('Blue'));  //true

indexOf(searchValue,fromIndex) //在字符串中查找searchValue第一次出現的index,fromIndex默認爲0,開始搜索的位置

console.log("Blue Whale".indexOf("Whale", 5));   //5
console.log("Blue Whale".indexOf("Whale", 12));  //-1

lastIndexOf(searchValue,fromIndex) 方法返回指定值在調用該方法的字符串中最後出現的位置,若是沒找到則返回 -1

console.log("canal".lastIndexOf("a"))  // returns 3
console.log("canal".lastIndexOf("a",7))  // returns 3

match() 當一個字符串與一個正則表達式匹配時, 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() 方法會用一個字符串填充當前字符串(若是須要的話則重複填充),返回填充後達到指定長度的字符串。從當前字符串的末尾(右側)開始填充。

console.log('abc'.padEnd(10));          // "abc       " 長度爲10
'abc'.padEnd(10, "foo");   // "abcfoofoof"  //長度爲10
'abc'.padEnd(6, "123456"); // "abc123" 長度爲6

es6padStart() 方法用另外一個字符串填充當前字符串(重複,若是須要的話),以便產生的字符串達到給定的長度。填充從當前字符串的開始(左側)應用的。

'abc'.padStart(10);         // "       abc"  長度爲10
'abc'.padStart(10, "foo");  // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"

repeat()構建並返回一個新字符串,

console.log('abcd'.repeat(2));   //abcdabcd
console.log('abcd'.repeat(0)); //''
console.log('abcd'.repeat(3.5)); //abcdabcdabcd 小數會進行一個求餘轉整數

replace() 匹配元素替換

console.log('hi word'.replace('hi','hello'))  //hello word

search() 方法執行正則表達式和 String對象之間的一個搜索匹配。

console.log('abc'.search('b'))  //下標爲1

slice(beginSlice,endSlice) 方法提取一個字符串的一部分,並返回新的字符串

console.log('abc'.slice(1,3))   //bc

split();把字符串根據符號改成數組

console.log('hello word'.split(''));[ 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd' ]

es6 startsWith(searchString,position) 判斷字符串開始是否以指定字符串

searchString 指定字符串
position 開始的位置  默認爲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() 方法返回一個字符串中從指定位置開始到指定字符數的字符。

console.log('hello word'.substr(1,2))  //el

substring() 方法返回一個字符串在開始索引到結束索引之間的一個子集, 或從開始索引直到字符串的末尾的一個子集。

var anyString = "Mozilla";
console.log(anyString.substring(0,3)); //Moz
console.log(anyString.substring(3,0)); //Moz

toLocaleLowerCase() 字符串轉換爲小寫

console.log('ALPHABET'.toLocaleLowerCase());  //alphabet

toLocaleUpperCase() 字符串轉換爲大小寫

console.log('alphabet'.toLocaleUpperCase()); //ALPHABET

toLowerCase() 轉換爲小寫

console.log('ALPHABET'.toLowerCase());  //alphabet

toUpperCase()轉換爲大寫

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() 是一個模板字符串的標籤函數

let name='xiaozhang';
console.log(String.raw`hello ${name}`); //hello xiaozhang

經常使用的轉義符號

\0    空字符
 \'    單引號
 \"    雙引號
 \\    反斜槓
 \n    換行
 \r    回車
 \v    垂直製表符
 \t    水平製表符
 \b    退格
 \f    換頁
 \uXXXX    unicode 碼
 \u{X} ... \u{XXXXXX}    unicode codepoint 
 \xXX    Latin-1 字符(x小寫)

今天就先寫到這裏,但願你們喜歡,也但願你們指點錯誤,也能夠加入qq羣439667347,你們一塊兒討論,一塊兒進步,後續更新中...java

相關文章
相關標籤/搜索