1. 程式人生 > >Js字串常用操作總結

Js字串常用操作總結

String型別

String型別是字串的物件包裝型別,可以使用String建構函式來建立

var stringObject = new String("hello world");

1.字元方法

charAt():以單字元字串的形式返回給定位置的字元
charCodeAt(): 返回該位置字元的字元編碼
是用於訪問字串中特定字元的方法,都接收一個引數,即基於0的字元位置。

2.字串操作方法

contact():用於將一個或者多個字串拼接起來,返回拼接得到的新字串
contact()方法可以接受任意多個引數,也就是說可以通過它拼接任意多個字串。
ECMAScript還提供了三個基於字串建立新字串的方法:slice()、substr()、substring()


這三個方法都會返回被操作字串的一個子字串,而且也都接受一或兩個引數。
第一個引數指定字串的開始位置,第二個引數(在特定情況下)表示字串到哪裡結束。
具體來說,slice()和substring()的第二個引數指定的是子字串最後一個字元後面的位置,而substr()的第二個引數指定的則是返回的字元個數。如果沒有給這些方法傳遞第二個引數,則將字串的長度作為結束位置。同樣,這三個方法不會修改字串本身的值。

var stringValue = "Hello world";
console.log(stringValue.slice(3));//"lo world"
console.log(stringValue.substring(3
));//"lo world" console.log(stringValue.substr(3));//"lo world" console.log(stringValue.slice(3,7));//"lo w" console.log(stringValue.substring(3,7));//"lo w" console.log(stringValue.substr(3,7));//"lo worl" //substr(3,7)從3開始返回7個字元,空格也佔位

在給slice()和substr()傳遞一個負值引數時,他們的行為相同(加上字串的長度),但substring()會返回全部字串因為會將負數轉化為0.

var stringValue = "hello world";
console.log(stringValue.slice(-3));//"rld"
console.log(stringValue.substring(-3));//"hello world"
console.log(stringValue.substr(-3));//"rld"
console.log(stringValue.slice(3,-4));//"lo w"(3,7)
console.log(stringValue.substring(3,-4));//"hel"(3,0)
console.log(stringValue.substr(3,-4));//""(空字串)

substring()引數為負數時,會自動轉化為0.

3.字串位置方法

從字串中查詢子字串的方法:indexOf()和lastIndexOf().從一個字串中搜索給定的子字串,然後返回子字串的位置(如果沒有找到該子字串,則返回-1)indexOf()從前向後搜尋,lastIndexOf()從後向前搜尋。
接受可選的第二個引數,表示開始搜尋的位置。

4.trim()方法

ECMAScript為所有字串定義了trim()方法。這個方法會建立一個字串的副本,刪除前置及字尾的所有空格,然後返回結果。

var stringValue = "  hello world   ";
var trimmedStringValue = stringValue.trim();
console.log(stringValue);//"  hello world   "
console.log(trimmedStringValue);//"hello world"

只刪除前後綴空格

5.字串大小寫轉換方法

toLowerCase()
toLocaleLowerCase()
toUpperCase()
toLocaleUpperCase()

toLocaleLowerCase()和toLocaleUpperCase()是針對特定地區的實現

6.字串的模式匹配方法

String型別定義了幾個用於在字串中匹配模式的方法。
match()本質上與RegExp的exec()方法相同。match()方法只接受一個引數,正則表示式或者是一個RegExp物件

var text = "cat,bat,sat,fat";
var pattern = /.at/;

//與pattern.exec(text)相同
var matches = text.match(pattern);
console.log(matches.index);//0
console.log(matches[0]);//"cat"
console.log(pattern.lastIndex);//0
Array[1]
0:"cat"
index:0
input:"cat,bat,sat,fat"
length:1

本例中match()方法返回了一個數組,陣列的第一項是與整個模式匹配的字串,之後的每一項(如果有)儲存著與正則表示式中的捕獲組匹配的字串。
match 方法返回的陣列有三個屬性:input、index和lastIndex
Input 屬性包含整個的被查詢字串。Index 屬性包含了在整個被查詢字串中匹配的子字串的位置。LastIndex 屬性包含了最後一次匹配中最後一個字元的下一個位置。
另一個用於查詢模式的方法是search(),這個方法的唯一引數與match()方法的引數相同:由字串或RegExp物件指定的一個正則表示式。
search()方法返回字串中第一個匹配項的索引:如果沒有找到匹配項,則返回-1.而且,search()方法始終是從字串開頭向後查詢模式。

var text = "cat,bat,sat,fat";
var pos = text.search(/at/);//引數:正則表示式
console.log(pos);//1

replace()方法,接受兩個引數:第一個引數可以是一個RegExp物件或者一個字串,第二個引數可以是一個字串或者一個函式。
如果第一個引數是字串,那麼只會替換第一個子字串。要想替換所有字串,唯一的辦法就是提供一個正則表示式,需要指定全域性(g)標誌。

var text = "cat,bat,sat,fat";
var result = text.replace("at","ond");
console.log(result);//"cond,bat,sat,fat"

result = text.replace(/at/g,"ond");
console.log(result);//"cond,bond,sond,fond" 

split()字串分隔函式
可以基於指定的分隔符將一個字串分隔成多個字串,並將結果放在一個數組中。
該方法接受可選的第 二個引數,用於指定陣列的大小,以確保返回的陣列不會超過既定大小。

localeCompare():用於比較兩個字串

var stringValue = "yellow";
console.log(stringValue.localeCompare("brick"));//1,y<b
console.log(stringValue.localeCompare("yellow"));//0,y=y
console.log(stringValue.localeCompare("zoo"));//-1,y>z

fromCharCode()方法接收一或多個字元編碼,然後將他們轉換成一個字串。