String類型
一、字符方法
chart()、charCodeAt()
這兩個方法都接收一個參數,即基於0的字符位置
var stringValue="hello world";
alert(stringValue.chartAt(1)); //"e" 得到的是字符
alert(stringValue.chartCodeAt(1)); //"101" 得到的是字符編碼
二、字符串操作方法
concat()、slice()、substr()、substring()
1.concat()用於將一或多個字符串拼接起來,返回拼接得到的新字符串。
var stringValue="hello ";
var result=stringValue.concat("world");
alert(result); //"hello world"
alert(stringValue); //"hello"
concat()方法可以接受任意多個參數,也就是說可以通過它拼接任意多個字符串。
2.ECMAScript還提供了三個基於子字符串創建新字符串的方法:slice()、substr()和substring()
這三個方法都會返回被操作字符串的一個子字符串,而且也都接收一個或兩個參數。第一個參數指定子字符串的開始位置,第二個參數表示在哪裏結束。具體來說,slice()和substring()的第二個參數指定的是子字符串最後一個字符後面的位置。而substr()的第二個參數指定的則是返回的字符個數。
var stringValue="Hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3,7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3,7)); //"lo worl"
當參數是負數時,slice()方法會將傳入的負值與字符串的長度相加。substr()方法將負的第一個參數加上字符串的長度,而將負的第二個參數轉換爲0。最後,substring()方法會把所有負值參數都轉化爲0。例子如下:
var stringValue="hello world";
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"rld"
alert(stringValue.slice(3,-4)); //"lo w"
alert(stringValue.substring(3,-4)); //"hel"
alert(stringValue.substr(3,-4)); //""空字符
3、字符串位置方法
indexOf()、lastIndexOf()
indexOf()方法從字符串的開頭向後搜索子字符串,而lastIndexOf()方法是從字符串的末尾向前搜索子字符串。
var stringValue="hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
alert(stringValue.indexOf("o",6)); //7
alert(stringValue.lastIndexOf("o",6)); //4
在使用第二個參數的情況下,可以通過循環調用indexOf()或lastIndexOf()來找到所有匹配的子字符串,如下面的例子所示:
var stringValue="Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions=new Array();
var pos=stringValue.indexOf("e");
while(pos>-1){
positions.push(pos);
pos=stringValue.indexOf("e",pos+1);
}
alert(position); //"3,24,32,35,52"
4、trim()方法
這個方法會創建一個字符串的副本,刪除前置及後綴的所有空格,然後返回結果
var stringValue=" hello world ";
var trimmedStringValue=stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue); //"hello world"
5、字符串大小寫轉換方法
toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()
6、字符串的模式匹配方法
match()、search()、replace()、split()
match()方法只接受一個參數,要麼是一個正則表達式,要麼是一個RegExp對象。
var text="cat, bat, sat, fat";
var pattern=/.at/;
//與pattern.exec(text)相同
var matches=text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0
search()方法與match()方法的參數相同。search()方法返回字符串中第一個匹配項的索引,如果沒有找到匹配項,則返回-1。而且,search()方法始終是從字符串開頭向後查找模式。如下:
var text="cat, bat, sat, fat";
var pos=text.search(/at/);
alert(pos); //1
replace()方法接受兩個參數:第一個參數可以是一個RegExp對象或者一個字符串(這個字符串不會被轉換成正則表達式),第二個參數可以使一個字符串或者一個函數。如果第一個參數是字符串,那麼只會替換第一個子字符串。要想替換所有子字符串,唯一的辦法就是提供一個正則表達式,而且要指定全局(g)標誌,如下:
var text="cat, bat, sat, fat";
var result=text.replace("at","ond");
alert(result); //"cond, bat, sat, fat"
result=text.replace(/at/g,"ond");
alert(result); //"cond, bond, sond, fond"
replace()方法的第二個參數也可以是一個函數。在只有一個匹配項的情況下,會向這個函數傳遞3個參數:模式的匹配項、模式匹配項在字符串中的位置和原始字符串。例子如下:
function htmlEscape(text){
return text.replace(/[<>"&]/g,function(match,pos,originalText){
switch(match){
case "<" : return "<";
case ">" : return ">";
case "&" : return "&";
case "\"" : return """;
}
});
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
//<p class="greeting">Hello world!</p>
splite()方法
var colorText="red,blue,green,yellow";
var colors1=colorText.splite(","); //["red","blue","green","yellow"]
var colors2=colorText.splite(",",2); //["red","blue"]
var colors3=colorText.splite(/[^\,]+/); //["" , "," , "," , "," , ""]
需要注意的是,在最後一次調用split()返回的數組中,第一項和最後一次項是兩個空字符串。之所以這樣,是因爲通過正則表達式指定的分隔符出現在了字符串的開頭(即子字符串”red”)和末尾(即子字符串”yellow”)。
7.localeCompare()方法
- 如果字符串在字母表中應該排在字符串參數之前,則返回一個負數(大多數情況下是-1,具體的值要視實現而定)
- 如果字符串等於字符串參數,則返回0
- 如果字符串在字母表中應該排在字符串參數之後,則返回一個正數(大多數情況下是1,具體的值要視實現而定)
var stringValue="yellow";
alert(stringValue.localeCompare("brick")); //1
alert(stringValue.localeCompare("yellow")); //0
alert(stringValue.localeCompare("zoo")); //-1
8、fromCharCode()方法
這個方法的任務是接收一或多個字符編碼,然後將他們轉換成一個字符串
alert(String.fromCharCode(104,101,108,108,111)); //"hello"