學習javascript的過程當中,老是容易string忘記方法,把字符串的一些方法所有彙總在一塊兒,方便查看和增長記憶.javascript
let str='hello word' 字符串
let number=0; //數字類型 console.log(String(number)) //0 字符串 console.log(new String(number)) //[String: '0'] 對象形式
console.log(String.fromCharCode(65,66,67)) //ABC
String.fromCodePoint(65, 90); // "AZ"
console.log('hello'.charAt(3)) //l
console.log("ABC".charCodeAt(0)) // returns 65
console.log("ABC".codePointAt(0)) // returns 65
let hello = 'hello'; console.log(hello.concat(' word','!')); //hello word!
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
console.log('Blue Whale'.includes('blue')); //false 區分大小寫 console.log('Blue Whale'.includes('Blue')); //true
console.log("Blue Whale".indexOf("Whale", 5)); //5 console.log("Blue Whale".indexOf("Whale", 12)); //-1
console.log("canal".lastIndexOf("a")) // returns 3 console.log("canal".lastIndexOf("a",7)) // returns 3
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' ]
console.log('abc'.padEnd(10)); // "abc " 長度爲10 'abc'.padEnd(10, "foo"); // "abcfoofoof" //長度爲10 'abc'.padEnd(6, "123456"); // "abc123" 長度爲6
'abc'.padStart(10); // " abc" 長度爲10 'abc'.padStart(10, "foo"); // "foofoofabc" 'abc'.padStart(6,"123465"); // "123abc"
console.log('abcd'.repeat(2)); //abcdabcd console.log('abcd'.repeat(0)); //'' console.log('abcd'.repeat(3.5)); //abcdabcdabcd 小數會進行一個求餘轉整數
console.log('hi word'.replace('hi','hello')) //hello word
console.log('abc'.search('b')) //下標爲1
console.log('abc'.slice(1,3)) //bc
console.log('hello word'.split(''));[ 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd' ]
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
console.log('hello word'.substr(1,2)) //el
var anyString = "Mozilla"; console.log(anyString.substring(0,3)); //Moz console.log(anyString.substring(3,0)); //Moz
console.log('ALPHABET'.toLocaleLowerCase()); //alphabet
console.log('alphabet'.toLocaleUpperCase()); //ALPHABET
console.log('ALPHABET'.toLowerCase()); //alphabet
console.log('alphabet'.toUpperCase()) //ALPHABET
console.log(' hello '.trim()); //hello
let string=new String('hello word'); console.log(string); //[String: 'hello word'] console.log(string.valueOf()) //hello word
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