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()方法接收一或多個字符編碼,然後將他們轉換成一個字符串。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章