javascript string 方法總結

字符方法

一、charAt()
接收一個參數,基於0的字符位置。以單字符串的形式返回給定位置的那個字符。javascript

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

二、charCodeAt()
接收一個參數,基於0的字符位置。 返回的是字符編碼。java

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

字符串操做方法

一、concat()
用於將一個或多個字符串拼接起來,返回拼接獲得的新字符串,不會修改字符串自己的值,只是返回一個基本類型的字符串值。正則表達式

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

二、slice()
截取字符串,只是返回一個基本類型的字符串值,對原始字符串沒有任何影響。
若是傳兩個參數,第一個參數是開始截取的位置,第二個參數是結束截取的位置。函數

var stringValue = "hello world";
   console.log(stringValue.slice(3)); //"lo world"
   console.log(stringValue.slice(3,7)); //"lo w"

三、substring()
截取字符串,只是返回一個基本類型的字符串值,對原始字符串沒有任何影響。
若是傳兩個參數,第一個參數是開始截取的位置,第二個參數是結束截取的位置。編碼

var stringValue = "hello world";
   console.log(stringValue.substring(3)); //"lo world"
   console.log(stringValue.substring(3,7)); //"lo w"

四、substr()
截取字符串,只是返回一個基本類型的字符串值,對原始字符串沒有任何影響。
若是傳兩個參數,第一個參數是開始截取的位置,第二個參數是返回的字符個數。code

var stringValue = "hello world";
   console.log(stringValue.substr(3)); //"lo world"
   console.log(stringValue.substr(3,7)); //"lo worl"

字符串位置方法

一、indexOf()
接收一個參數的時候,返回第一次出現該字符的位置
接收兩個參數的時候,第一個是查找的字符,第二個是開始查找的位置。對象

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

二、lastIndexOf()
接收一個參數的時候,返回最後一次出現該字符的位置
接收兩個參數的時候,第一個是查找的字符,第二個是開始查找的位置。索引

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

trim()方法

這個方法會建立一個字符串的副本,刪除前置及後綴的全部空格,而後返回結果。ip

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

字符串大小寫轉換方法

一、toLowerCase()
將字符串轉換成小寫字符串

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

二、toUpperCase()
將字符串轉換成大寫

var stringValue = "hello world";
   console.log(stringValue.toUpplerCase()); //"HELLO WORLD"

字符串的模式匹配方法

一、match()
只接受一個參數,要麼是一個正則表達式,要麼是一個RegExp對象。

var text = "cat,bat,sat,fat";
   var pattern = /.at/;
   
   var matches = text.match(pattern);
   console.log(maches.index); //0
   console.log(maches[0]); //"cat"

二、search()
惟一參數與match()方法參數相同,search()方法返回字符串中第一個匹配項的索引;若是沒有找到匹配項,則返回-1.

var text = "cat, bat, sat, fat";
   var pos = text.search(/at/);
   console.log(pos); //1

三、replace()
這個方法接收兩個參數:第一個參數能夠使一個RegExp對象或者一個字符串,第二個參數能夠使一個字符串或者一個函數。

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"

localeCompare()方法

這個方法比較兩個字符串,並返回下列值中的一個:

  • 若是字符串在字母表中應該在字符串參數以前,則返回一個負數。
  • 若是字符串等於字符串參數,則返回0
  • 若是字符串在字母表中應該排在字符串參數以後,則返回一個正數。
var stringValue = "yellow";
   console.log(stringValue.localeCompare("brick")); //1
   console.log(stringValue.localeCompare("yellow")); //0
   console.log(stringValue.localeCompare("zoo")); //-1

fromCharCode()方法

這個方法的任務是接收一個或者多個字符編碼,而後將它們轉換成一個字符串。

console.log(String.fromCharCode(104,101,108,108,111)); //"hello"
相關文章
相關標籤/搜索