js字串函式 1

2022-09-17 06:33:10 字數 1068 閱讀 3271

js自帶函式

concat

將兩個或多個字元的文字組合起來,返回乙個新的字串。

var a = "hello";

var b = ",world";

var c = a.concat(b);

alert(c);

//c = "hello,world"

indexof

返回字串中乙個子串第一處出現的索引(從左到右搜尋)。如果沒有匹配項,返回 -1 。

var index1 = a.indexof("l");

//index1 = 2

var index2 = a.indexof("l",3);

//index2 = 3

charat

返回指定位置的字元。

var get_char = a.charat(0);

//get_char = "h"

lastindexof

返回字串中乙個子串最後一處出現的索引(從右到左搜尋),如果沒有匹配項,返回 -1 。

var index1 = lastindexof('l');

//index1 = 3

var index2 = lastindexof('l',2)

//index2 = 2

match

檢查乙個字串匹配乙個正規表示式內容,如果麼有匹配返回 null。

var re = new regexp(/^\w+$/);

var is_alpha1 = a.match(re);

//is_alpha1 = "hello"

var is_alpha2 = b.match(re);

//is_alpha2 = null

substring

返回字串的乙個子串,傳入引數是起始位置和結束位置。

var sub_string1 = a.substring(1);

//sub_string1 = "ello"

var sub_string2 = a.substring(1,4);

//sub_string2 = "ell"

js字串函式

1 charcodeat方法返回乙個整數,代表指定位置字元的unicode編碼。strobj.charcodeat index 說明 index將被處理字元的從零開始計數的編號。有效值為0到字串長度減1的數字。如果指定位置沒有字元,將返回nan。例如 var str abc str.charcode...

js字串函式

concat 將兩個或多個字元的文字組合起來,返回乙個新的字串。var a hello var b world var c a.concat b alert c c hello,world indexof 返回字串中乙個子串第一處出現的索引 從左到右搜尋 如果沒有匹配項,返回 1 var index...

JS字串函式

concat 將兩個或多個字元的文字組合起來,返回乙個新的字串。var a hello var b world var c a.concat b alert c c hello,world indexof 返回字串中乙個子串第一處出現的索引 從左到右搜尋 如果沒有匹配項,返回 1 var index...