js字串方法

2021-08-03 15:11:57 字數 2814 閱讀 6161

關鍵字: js字串方法

js**

js自帶函式

● concat

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

vara="hello";

varb=",world";

varc=

alert(c);

//c="hello,world"

● indexof

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

varindex1=a.indexof("l");

//index1=2

varindex2=a.indexof("l",3);

//index2=3

● charat

返回指定位置的字元。

varget_char=a.charat(0);

//get_char="h"

● lastindexof

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

varindex1=lastindexof('l');

//index1=3

varindex2=lastindexof('l',2)

//index2=2

● match

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

varre=newregexp(/^\w+$/);

varis_alpha1=a.match(re);

//is_alpha1="hello"

varis_alpha2=b.match(re);

//is_alpha2=null

● substring

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

varsub_string1=a.substring(1);

//sub_string1="ello"

varsub_string2=a.substring(1,4);

//sub_string2="ell"

● substr

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

varsub_string1=a.substr(1);

//sub_string1="ello"

varsub_string2=a.substr(1,4);

//sub_string2="ello"

● replace

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

varresult1=a.replace(re,"hello");

//result1="hello"

varresult2=b.replace(re,"hello");

//result2=",world"

● search

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

varindex1=a.search(re);

//index1=0

varindex2=b.search(re);

//index2=-1

● slice

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

varsub_string1=a.slice(1);

//sub_string1="ello"

varsub_string2=a.slice(1,4);

//sub_string2="ell"

● split

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

vararr1=a.split("");

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

● length

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

varlen=a.length();

//len=5

● tolowercase

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

varlower_string=a.tolowercase();

//lower_string="hello"

● touppercase

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

varupper_string=a.touppercase();

//upper_string="hello"

/* 字串函式擴充

*//*

//去除左邊的空格

*/string.prototype.ltrim=function()

/*//去除右邊的空格

*/string.prototype.rtrim=function()

/*//去除前後空格

*/string.prototype.trim=function()

/*//得到左邊的字串

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

else }

returnthis.substr(0,len);

} /*

//得到右邊的字串

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

else }

returnthis.substring(this.length-len,this.length);

} /*

//得到中間的字串,注意從0開始

*/string.prototype.mid=function(start,len)

/*//在字串裡查詢另一字串:位置從0開始

*/string.prototype.instr=function(str)

{ if(str==null)

js字串方法

關鍵字 js字串方法 js js自帶函式 concat 將兩個或多個字元的文字組合起來,返回乙個新的字串。vara hello varb world varc alert c c hello,world indexof 返回字串中乙個子串第一處出現的索引 從左到右搜尋 如果沒有匹配項,返回 1。va...

字串分割方法總結

字串分割 c 經常碰到字串分割的問題,這裡總結下,也方便我以後使用。一 用strtok函式進行字串分割 原型 char strtok char str,const char delim 功能 分解字串為一組字串。引數說明 str為要分解的字串,delim為分隔符字串。返回值 從str開頭開始的乙個個...

vc字串大全

vc字串大全.txt52每個人都一條拋物線,天賦決定其開口,而最高點則需後天的努力。沒有秋日落葉的飄零,何來新春綠芽的餓明麗?只有懂得失去,才會重新擁有。vc字串函式大全 bcmp 比較記憶體內容 相關函式 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncas...