js 自帶字串函式

2021-08-26 06:01:57 字數 3120 閱讀 8372

js字串函式***

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"

substr

返回字串的乙個子串,傳入引數是起始位置和長度

var sub_string1 = a.substr(1);

//sub_string1 = "ello"

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

//sub_string2 = "ello"

replace

用來查詢匹配乙個正規表示式的字串,然後使用新字串代替匹配的字串。

var result1 = a.replace(re,"hello");

//result1 = "hello"

var result2 = b.replace(re,"hello");

//result2 = ",world"

search

執行乙個正規表示式匹配查詢。如果查詢成功,返回字串中匹配的索引值。否則返回 -1 。

var index1 = a.search(re);

//index1 = 0

var index2 = b.search(re);

//index2 = -1

slice

提取字串的一部分,並返回乙個新字串(與 substring 相同)。

var sub_string1 = a.slice(1);

//sub_string1 = "ello"

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

//sub_string2 = "ell"

split

通過將字串劃分成子串,將乙個字串做成乙個字串陣列。

var arr1 = a.split("");

//arr1 = [h,e,l,l,o]

length

返回字串的長度,所謂字串的長度是指其包含的字元的個數。

var len = a.length();

//len = 5

tolowercase

將整個字串轉成小寫字母。

var lower_string = a.tolowercase();

//lower_string = "hello"

touppercase

將整個字串轉成大寫字母。

var upper_string = a.touppercase();

//upper_string = "hello"

/*******************************************

字串函式擴充

******************************************

*//*

****************************************===

//去除左邊的空格

****************************************===

*/string.prototype.ltrim = function()

/*****************************************===

//去除右邊的空格

****************************************===

*/string.prototype.rtrim = function()

/*****************************************===

//去除前後空格

****************************************===

*/string.prototype.trim = function()

/*****************************************===

//得到左邊的字串

****************************************===

*/string.prototype.left = function(len)

else

}return this.substr(0,len);

}

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...