天天看點

JS字元串方法都有哪些

字元串方法和屬性

字元串長度

length

屬性傳回字元串的長度:

var str = 'jefighnibrpbk';
var res = str.length;
           

查找字元串中的字元串

indexOf()

方法傳回字元串中指定文本首次出現的索引(位置):

var str = "China 100th Anniversary of the Founding of the People's Republic of China";
var res = str.indexOf('China');
console.log(res); //0
           

lastIndexOf()

方法傳回指定文本在字元串中最後一次出現的索引(位置)

var str = "China 100th Anniversary of the Founding of the People's Republic of China";
var res = str.lastIndexOf("China");
console.log(res); //68
           

如果未找到文本,

indexOf()

lastIndexOf()

均傳回

-1

var str = "China 100th Anniversary of the Founding of the People's Republic of China";
var res = str.indexOf('USA');
console.log(res); //-1
           

兩種方法都

接受

作為檢索起始位置的

第二個參數

indexOf()

方法向後進行檢索(

從頭到尾

),這意味着:假如第二個參數是 10,則從位置 10 開始檢索,直到字元串的終點

lastIndexOf()

方法向後進行檢索(

從尾到頭

),這意味着:假如第二個參數是 50,則從位置 50 開始檢索,直到字元串的起點

var res = str.indexOf("China", 10);
var res = str.lastIndexOf("China", 50);
           

檢索字元串中的字元串

search()

方法搜尋特定值的字元串,并傳回比對的位置

var str = "The full name of China is the People's Republic of China.";
var res = str.search("name");
           

兩種方法,

indexOf()

search()

,是相等的。

差別在于:

  • search() 方法無法設定第二個開始位置參數
  • indexOf() 方法無法設定更強大的搜尋值(正規表達式)

提取部分字元串

有三種提取部分字元串的方法:

  • slice(start, end)
  • substring(start, end)
  • substr(start, length

slice()

提取字元串的某個部分并在新字元串中傳回被提取的部分。

// 位置 7 到位置 13 的片段
var res = str.slice(7,13); // Banana

// 如果某個參數為負,則從字元串的結尾開始計數。
var res = str.slice(-13,-7); // Banana

// 如果省略第二個參數,則該方法将裁剪字元串的剩餘部分:
var res = str.slice(7);

// 從結尾計數
var res = str.slice(-13);
           

substring()

類似于

slice()

// 不同之處在于 substring() 無法接受負的索引。
var res = str.substring(7,13);

// 如果省略第二個參數,則該 substring() 将裁剪字元串的剩餘部分。
var res = str.substring(7);
           

substr()

類似于

slice()

// 不同之處在于第二個參數規定被提取部分的長度。
var res = str.substr(7,6);//Banana

// 如果省略第二個參數,則該 substr() 将裁剪字元串的剩餘部分。
var res = str.substr(7); //Banana, Mango

// 如果首個參數為負,則從字元串的結尾計算位置。
var res = str.substr(-5); // Mango
           
第二個參數不能為負,因為它定義的是長度。

替換字元串内容

replace()

方法用另一個值替換在字元串中指定的值。

str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School");
           

replace()

方法不會改變調用它的字元串。它傳回的是新字元串。

// 1.預設地,replace() 隻替換首個比對:
str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "W3School");
// Please visit W3School and Microsoft!

// 2.預設地,replace() 對大小寫敏感。是以不對比對 MICROSOFT:
str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT", "W3School");
// 不會變

// 3.如需執行大小寫不敏感的替換,請使用正規表達式 /i(大小寫不敏感):
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");

// 4.如需替換所有比對,請使用正規表達式的 g 标志(用于全局搜尋):
str = "Please visit Microsoft and Microsoft!";
var n = str.replace(/Microsoft/g, "W3School");
           

轉換為大寫和小寫

通過

toUpperCase()

把字元串轉換為

大寫

var text1 = "Hello World!";
var text2 = text1.toUpperCase();  // text2 是被轉換為大寫的 text1
           

通過

toLowerCase()

把字元串轉換為

小寫

var text1 = "Hello World!";
var text2 = text1.toLowerCase();  // text2 是被轉換為小寫的 text1
           

concat() 方法

concat()

連接配接兩個或多個字元串:

var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);

// concat() 方法可用于代替加運算符。下面兩行是等效的:
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ","World!");
           

所有字元串方法都會傳回新字元串。它們不會修改原始字元串。

正式地說:字元串是不可變的:字元串不能更改,隻能替換。

String.trim()

trim()

方法删除字元串兩端的空白符:

var str = "       Hello World!        ";
alert(str.trim());
           
Internet Explorer 8 或更低版本不支援 trim() 方法。

提取字元串字元

這是兩個提取字元串字元的安全方法:

charAt(position)

charCodeAt(position)

charAt()

方法傳回字元串中指定下标(位置)的字元串:

var str = "HELLO WORLD";
str.charAt(0);            // 傳回 H
           

charCodeAt()

方法傳回字元串中指定索引的字元 unicode 編碼:

var str = "HELLO WORLD";
str.charCodeAt(0);         // 傳回 72
           

把字元串轉換為數組

可以通過

split()

将字元串轉換為數組:

var txt = "a,b,c,d,e";   // 字元串
txt.split(",");          // 用逗号分隔
txt.split(" ");          // 用空格分隔
txt.split("|");          // 用豎線分隔
// ["a", "b", "c", "d", "e"]
// ["a,b,c,d,e"]
// ["a,b,c,d,e"]

var txt = "Hello";       // 字元串
txt.split("");           // 分隔為字元
// ["H", "e", "l", "l", "o"]
           

自己總結學習,如有沖突,請聯系删除。