字符串操作

1、.indexOf() 返回某個字符在字符串中首次出現的位置大小寫敏感。如果沒有,返回 -1 。

  • indexOf(value, index)
    • searchvalue  必需。 規定需檢索的字符串值。
    • index     可選。 整數,規定在字符串中開始檢索的位置。
例:
var char = "hello world everyone";
console.log( char.indexOf( 'w' ) );     // 6 
console.log( char.indexOf( 'w', 7 ) );  // 21  從位置7開始,world的'w'不在範圍內。
console.log( char.indexOf( 'world' ) ); // 6 

2、.charAt() 返回指定位置的字符。

  • stringObject.charAt(index)   index: 必需。字符在字符串中的下標。
var char = "hello world everyonesw";
console.log( char.charAt( 1 ) )  // e

3、.charCodeAt( index ) 返回指定位置的字符的 Unicode 編碼。這個返回值是 0 - 65535 之間的整數。

  • stringObject.charCodeAt( index )
    • index   必需。 表示字符串中某個位置的數字,即字符在字符串中的下標。
    •  如果 index 是負數,或大於等於字符串的長度,則 charCodeAt() 返回 NaN。
例:
var char = "hello world everyone";
console.log( char.charCodeAt( 1 ) );  // 101
console.log( char.charCodeAt( 2 ) );  // 108

4、.substring() 方法用於提取字符串中介於兩個指定下標之間的字符。

  • “stringObject.substring(start,stop)”
    • start 必需,非負整數,規定開始字符所在位置。
    • stop 可選,非負整數,規定結束字符所在位置。如果省略,則返回的子串會一直到字符串的結尾。
var char = "hello world everyonesw";
console.log( char.substring( 3 ) )  // lo world everyonesw  
console.log( char.substring( 0,1 ) )  // h 
console.log( char.substring( 0,21 ) )  // hello world everyones  

5.join() 方法用於把數組中的所有元素放入一個字符串。元素是通過指定的分隔符進行分隔的。返回一個字符串。

  • strObj.join(divide)
var arr = ["hello","I","like","this"];
console.log(arr.join(';'));  //hello;I;like;this

6.split() 方法把一個字符串分割成數組(join相反)。

  • strObj.split(separator,howmany)
    • separator 必需。字符串或正則表達式,從該參數指定的地方分割 stringObject。
    • howmany 可選。指定返回的數組的最大長度。子串<=改值。如果沒有設置,整個字符串都會被分割。
var str2 = "How are you doing today?";
console.log(str2.split(" ")); //["How", "are", "you", "doing", "today?"]
console.log(str2.split(""));  // ["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", " ", "d", "o", "i", "n", "g", " ", "t", "o", "d", "a", "y", "?"]
console.log(str2.split(" ", 3));  //  ["How", "are", "you"]

7、ECMAscript 沒有對該方法進行標準化,因此反對使用它。

  • stringObject.substr(start,length);
    • start 必需。要抽取的子串的起始下標。必須是數值。如果是負數,那麼該參數聲明從字符串的尾部開始算起的位置。也就是說,-1 指字符串中最後一個字符,-2 指倒數第二個字符,以此類推。

// length 可選。子串中的字符數。必須是數值。如果省略了該參數,那麼返回從 stringObject 的開始位置到結尾的字串。

var str = "hello world";
console.log(str.substr(1,4));  //(firefox&chrome&IE7) ello  
console.log(str.substr(-1,4));  //(firefox&chrome) d   (IE7) hell

8.concat() 方法用於連接兩個或多個數組(字符串)。該方法不會改變現有的數組,而僅僅會返回被連接數組的一個副本。

var str3 = "helllo";
var str4 = "world";
var arr1 = [3,5];
var arr2 = [6,7];
console.log(arr2.concat(arr1));  //[6, 7, 3, 5]
console.log(str3.concat(str4));  //hellloworld

9.大小寫轉換,IE7支持

與 toLowerCase() 不同的是,toLocaleLowerCase() 方法按照本地方式把字符串轉換爲小寫。只有幾種語言(如土耳其語)具有地方特有的大小寫映射,所有該方法的返回值通常與 toLowerCase() 一樣。

// 大轉小
var low = "HELLO WORLD";
console.log(low.toLowerCase());
console.log(low.toLocaleLowerCase());
// 小轉大
var pper = "hellow world";
console.log(pper.toUpperCase());
console.log(pper.toLocaleUpperCase());

10.match() 方法可在字符串內檢索指定的值,或找到一個或多個正則表達式的匹配。該方法類似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。

如果 regexp 沒有標誌 g,那麼 match() 方法就只能在 stringObject 中執行一次匹配。

不過全局匹配返回的數組的內容與前者大不相同,它的數組元素中存放的是 stringObject 中所有的匹配子串,而且也沒有 index 屬性或 input 屬性。

在全局檢索模式下,match() 即不提供與子表達式匹配的文本的信息,也不聲明每個匹配子串的位置。如果您需要這些全局檢索的信息,可以使用 RegExp.exec()。

var str="Hello world!";
console.log(str.match("world"));// ["world", index: 6, input: "Hello world!"]
console.log(str.match("World")); //null
console.log(str.match("worlld"));  //null
console.log(str.match("world!"));//["world", index: 6, input: "Hello world!"]
var patt5 = /\w/g;
console.log( str.match( patt5 ) ); //["H", "e", "l", "l", "o", "w", "o", "r", "l", "d"]

11.exec()

相同:在調用非全局的 RegExp 對象的 exec() 方法時,返回的數組與調用方法 String.match() 返回的數組是相同的。

不同:,無論 RegExpObject 是否是全局模式,exec() 都會把完整的細節添加到它返回的數組中。這就是 exec() 與 String.match() 的不同之處,後者在全局模式下返回的信息要少得多。

exec() 方法還返回兩個屬性。index 屬性聲明的是匹配文本的第一個字符的位置。input 屬性則存放的是被檢索的字符串 string。

var str = "Visit W3School or W3School and W2School"; 
var patt = /W2School/g;
var result = patt.exec(str);
console.log(result);  //

12.search() 方法用於檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。

返回值: stringObject 中第一個與 regexp 相匹配的子串的起始位置。

註釋:如果沒有找到任何匹配的子串,則返回 -1。search() 方法不執行全局匹配,它將忽略標誌 g

// 8.search()
var str8 = "hello world 34";
console.log(str8.search("world")); // 6
var patt = /[0-9]/g;
console.log(str8.search(patt));  // 12

13.replace() 方法用於在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。

var str = "hello world everyone";
console.log(str.replace(/hello/,'hi')); // hi world everyone
console.log(str.replace('hello','hi')); //hi world everyone

14.test() 方法用於檢測一個字符串是否匹配某個模式.如果字符串 string 中含有與 RegExpObject 匹配的文本,則返回 true,否則返回 false。

var str = "Visit W3School";
var patt1 = new RegExp("W3School");
var result = patt1.test(str);
console.log(result);
發佈了42 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章