字符串的一些用法總結

1. 兩個用於訪問字符串中特定字符 charAt() 和 charCodeAt() 

兩者都只有一個參數,不同的是前者返回字符,後者返回字符的字符編碼。 

var stringValue = "hello world";
console.log(stringValue.charAt(1)); //"e"

var stringValue = "hello world"; 
console.log(stringValue.charCodeAt(1)); //輸出"101" 

2. 字符串拼接方法 concat() 

用於將一或多個字符串拼接起來,返回拼接得到的新字符串,實戰中還是更多的使用 ‘+’ 操作符來拼接。

var stringValue = "hello "; 
var result = stringValue.concat("world"); 
console.log(result); //"hello world" 
console.log(stringValue); //"hello" 

3. 字符串創建方法 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" 

如過傳入的值爲負值,slice()方法會將傳入的負值與字符串的長度相加,substr() 方法將負的第一個參數加上字符串的長度,而將負的第二個參數轉換爲 0。最後,substring() 方法會把所有負值參數都轉換爲 0。下面來看例子。

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

 

4. 從字符串中查找子字符串的方法 indexOf() 和 lastIndexOf()

indexOf()方法從字符串的開頭向後搜索子字符串,而 lastIndexOf()方法是從字符串的末尾向前搜索子字符串。舉例:

var stringValue = "hello world"; 
console.log(stringValue.indexOf("o")); //4 
console.log(stringValue.lastIndexOf("o")); //7 

這兩個方法都可以接收可選的第二個參數,表示從字符串中的哪個位置開始搜索。

var stringValue = "hello world"; 
console.log(stringValue.indexOf("o", 6)); //7 
console.log(stringValue.lastIndexOf("o", 6)); //4 

5. trim()方法

刪除字符串前後的空格。舉例:

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

6. 字符串大小寫轉換方法 toLowerCase()、toLocaleLowerCase()、toUpperCase()和 toLocaleUpperCase()

 直接舉例:

var stringValue = "hello world"; 
console.log(stringValue.toLocaleUpperCase()); //"HELLO WORLD" 
console.log(stringValue.toUpperCase()); //"HELLO WORLD" 
console.log(stringValue.toLocaleLowerCase()); //"hello world" 
console.log(stringValue.toLowerCase()); //"hello world" 

7. 字符串的模式匹配方法  match()、search()、replace()

match()方法接受一個參數要麼是一個正則表達式,要麼是一個 RegExp 對象,與RegExp的exec() 方法相同。

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 

search()方法接受一個參數要麼是一個正則表達式,要麼是一個 RegExp 對象,如果匹配則返回第一次出現的位置,否則返回-1。

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" 

8. localeCompare()

與操作字符串有關的最後一個方法是 localeCompare(),這個方法比較兩個字符串,並返回下列
值中的一個:

  • 如果字符串在字母表中應該排在字符串參數之前,則返回一個負數(大多數情況下是-1,具體的值要視實現而定);
  • 如果字符串等於字符串參數,則返回 0;
  • 如果字符串在字母表中應該排在字符串參數之後,則返回一個正數(大多數情況下是 1,具體的值同樣要視實現而定)。

下面是幾個例子:

var stringValue = "yellow"; 
console.log(stringValue.localeCompare("brick")); //1 
console.log(stringValue.localeCompare("yellow")); //0 
console.log(stringValue.localeCompare("zoo")); //-1 

本文結。

 

 

 

 

 

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