字串的方法總結

2022-07-19 02:33:09 字數 3106 閱讀 1326

1. length 屬性返回字串的長度

var str = '1234567'

console.log(str.length) // 7

2. indexof() 方法返回字串中指定文字__首次__出現的索引

3. lastindexof() 方法返回字串中指定文字__最後__出現的索引

includes() 返回 true false

var str = 'china'

console.log(str.indexof('china')) // 0 如果是字串按乙個來

console.log(str.indexof('c')) // 0 返回的是下標 從 0 開始

console.log(str.indexof('w')) // -1 沒有則返回 -1

// 兩種方法都接受作為檢索起始位置的第二個引數 縮小搜查範圍

var str = "the full name of is the people's republic of china.";

console.log(str.indexof("china")); // 45

console.log(str.indexof("china", 11)); // 45

3. 有三種提取部分字串的方法:
var str = "0123456789"

var res = str.slice(1,4) // 123 注意包括__前__不包括__後__

var res = str.slice(1,-1); // 12345678 // 負值從左數 -1 不包含9

var res = str.slice(1); // 123456789 // 不寫 就接到最後了

var res = str.slice(-5); // 56789 // 簡單記憶 擷取後五位

4. 把字串轉換為陣列 split()
var str = "a,b,c,d,e,f";

var arr = str.split(","); // ["a", "b", "c", "d", "e", "f"]

// 反過來 用的是陣列的 join(',')

5. replace() 方法不會改變呼叫它的字串。它返回的是新字串
str = "please visit microsoft and microsoft!" 

var n = str.replace("microsoft", "w3school") // "please visit w3school and microsoft!"

str = "please visit microsoft and microsoft!" 

var n = str.replace("microsoft", "w3school") // "please visit microsoft and microsoft!"

str = "please visit microsoft and microsoft!" 

var n = str.replace(/microsoft/g, "w3school") // "please visit w3school and w3school!"

6. 大小寫 轉換 通過 touppercase() 把字串轉換為大寫 通過 tolowercase() 把字串轉換為小寫:
var text1 = "hello world!"       

var text2 = text1.tolowercase() // "hello world!"

7. 字串的拼接方法 concat() 連線兩個或多個字串
var text1 = "hello";

var text2 = "world";

text3 = text1.concat(" ",text2); // hello world

8. trim() 方法刪除字串兩端的空白符:
var str = "       hello world!        ";

alert(str.trim()); // hello world!

// internet explorer 8 或更低版本不支援 trim() 方法

// 如需支援 ie 8,您可搭配正規表示式使用 replace() 方法代替:

// var str = " hello world! ";

// alert(str.replace(/^[\s\ufeff\xa0]+|[\s\ufeff\xa0]+$/g, ''));

9. 通過下標 提取字串 charat(position) charcodeat(position)
var str = "hello world";

str.charat(0); // 返回 h

str.charcodeat(0); // 返回 72 這是'h'的 unicode 編碼

// 最好還是使用 str[0] // 更加直觀

* 使用屬性訪問有點不太靠譜:

* 不適用 internet explorer 7 或更早的版本

* 它讓字串看起來像是陣列(其實並不是)

* 如果找不到字元,[ ] 返回 undefined,而 charat() 返回空字串。

* 它是唯讀的。str[0] = "a" 不會產生錯誤(但也不會工作!) 賦值是無效的

10. 檢測以...開頭 以... 結尾 startwith() endwith() 返回 true false
11. 補位方法 padstart() padend()
var str = '12'

str.padstart(6, '0') // 000012

str.padend(6, '0') // 120000

12. 字串中加空格
var a = 'something' + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + 'something';

字串方法總結

doctype html en utf 8 viewport content width device width,initial scale 1.0 x ua compatible content ie edge document title head 屬性 str.length 獲取字串長度 方...

字串的處理方法總結

string類和stringbuffer類 1 string處理一些小的文字 stringbuffer處理大型文字 原因 用string處理大型文字,會消耗大量系統資源。2 string類一旦產生字串,某物件不可變。雖通過各種系統方法操作字串,但不改變物件本身,而產生新的字串。stringbuffe...

python 字串的方法總結

test bbc dea test1 test2 test3 user temail tpass nwang t123 qq.com t123 nwang t123 qq.com t123 首字母大寫,其餘小寫 v1 test.capitalize print v1 結果 bbc dea 所有字母變...