字符方法
chatAt()和chatCodeAt
charAt()和charCodeAt()
這兩個方法接收一個參數,即基於0的字符位置。
chatAt()以單字符字符串的形式返回給定位置的那個字符。
chatCodeAt()返回給定位置的那個字符的字符編碼。
var stringValue="hellow";
stringValue.chatAt(1); //"e"
stringValue.chatCodeAt(1); //"101"
字符串操作方法
concat()
concat()用於將一個或多個字符串拼接起來,返回拼接得到的新創建的字符串。
但實踐中大多數情況還是使用(+)加號操作符,比concat()簡單易行。
slice()、substring()、substr()
三個基於子字符串創建新字符串的方法,創建並返回被操作字符串的子字符串。注意方法名稱都是小寫
共同點:
參數1:必選,指定子字符串開始位置。
參數2:可選,不指定時將字符串的長度作爲結束位置。
差異:
1、當指定參數2時
方法 | 參數2含義 |
---|---|
slice()、substring() | 指定子字符串最後一個字符的位置 |
substr | 返回或截取的字符個數 |
var stringValue="hello world";
stringValue.length; //11
stringValue.slice(3); //"lo world"
stringValue.substring(3); //"lo world"
stringValue.substr(3); //"lo world"
stringValue.slice(3,7); //"lo wo"
stringValue.substring(3,7); //"lo wo"
stringValue.substr(3,7); //"lo worl"
2、當傳遞這些方法的參數是負數時
方法 | 行爲 |
---|---|
slice() | 所有傳入的負值與字符串的長度相加 |
substring() | 所有的負值參數都轉化爲0 |
substr() | 將負的第一個參數加上字符串的長度,將負的第二個參數轉換爲0 |
var stringValue="hello world";
stringValue.length; //11
stringValue.slice(-3); //"rld"
stringValue.substring(-3); //"hello world"
stringValue.substr(-3); //"rld"
stringValue.slice(3,-4); //"lo wo"
stringValue.substring(3,-4); //"hel"
stringValue.substr(3,-4); //""
字符串位置方法
indexOf()和lastIndexOf
從字符串中查找子字符串的位置,如果找到返回子字符串的位置;找不到返回-1
參數1:指定檢索的子字符串
參數2:可選,指定開始檢索的位置
indexOf()從開頭向末尾搜索
lastIndexOf()從末尾向開頭搜索
stringValue="hello world";
stringValue.indexOf('o',6); //7
stringValue.lastIndexOf('o',6); //4,此時從指定位置向前檢索
trim()方法
創建一個字符串的副本,刪除前置及後綴的所有空格,然後返回結果。
注意:字符串中的空格不管
stringValue=" hellow world ";
stringValue.trim(); //"hellow world"
stringValue; //" hellow world ";
字符串大小寫轉換方法
toLowerCase()和toUpperCase()
字符串模式匹配方法
match()
在字符串調用這個方法,本質上與調用RegExp對象的exec()方法相同。
參數:match()接收一個參數,要麼是正則表達式,要麼是正則對象。
返回值:matches返回一個數組,數組的第一項是與整個模式匹配的字符串,之後的每一項(如果有)保存着與正則表達式中捕獲組匹配的字符串。
var text="cat,bat,hat";
var pattern=/.at/;
var matches=text.match(pattern);
matches.index //0
matches[0] //"cat"
pattern.lastIndex //0
search()
參數:search()接收一個參數,要麼是正則表達式,要麼是正則對象。
返回值:返回字符串中第一個匹配項的索引,如果沒有找到匹配項,則返回-1.
注意:search()方法始終從字符串開頭向後查找模式
例子:
var text="cat,bat,hat";
var pos=text.search(/cat/); //0
replace()
誕生原因:爲了簡化替換子字符串的操作,ECMAScript提供了replace()方法
參數:第一個參數正則對象或者字符串,第二個參數是一個字符串或者函數。
返回值:生成的替換後的字符串
注意
text="cat,bat,hat";
result=text.replace("at","ond") //"cond,bat,hat"
result=text.replace(/at/g,"ond") //"cond,cont,hont"
更精細的替換操作,使用函數作爲replace()的第二個參數
在只有一個匹配項的情況下,會向這個函數傳遞3個參數:模式的匹配項、模式的匹配項在字符串中的位置、原始字符串。
在正則表達式中定義了多個捕獲組的情況下,這個函數的參數依次是:模式的匹配項、第一個捕獲組的匹配項、第二個捕獲組的匹配項...,最後兩個參數是模式的匹配項在字符串中的位置、原始字符串。
這個函數應該返回一個字符串,表示準備替換的預備項。
例子:
function htmlEscape(text){
return text.replace(/[<>"&]/g,function(match,pos,originText){
switch(case){
case "<":
return <
case ">":
return >
case "\"":
return "
case "&":
return &
}
})
}
htmlEscape("<p class=\"desc\">hellow</p>")
//輸出 <p class="desc;">hellow</p>
split()
基於指定的分割符將字符串分割成多個子字符串,並將結果存放在一個數組中。
參數:第一個參數是字符串或者正則表達式,匹配的項將作爲分隔符
第二個參數可選,用於指定數組的大小
color="red,blue,pink";
colors1=color.split(","); //["red","blue","pink"]
colors2=color.split(",",2); //["red","blue"]
colors3=color.split(/[^,]+/);
//["",",",",",""] 由於/^,/匹配的是除逗號以外的一個或多個字符,而"red"和"pink"剛好在開頭和結尾,因此數組第一個元素和最後一個爲空字符