當前位置:才華齋>設計>網頁設計>

javascript字串物件常用api函式小結

網頁設計 閱讀(2.62W)

1. concat(str1,str2,···)

javascript字串物件常用api函式小結

連線字串

2. indexOf(str,start)

返回 str 在字串中首次出現的位置

var str = "hello world";xOf("hello"); // xOf("o",5); // xOf("World"); // -1

3. lastIndexOf(str,start)

返回 str 在字串中最後出現的位置

var str = "hello world";IndexOf("hello"); // IndexOf("o",3); // IndexOf("o",5); // 4

4. replace(regexp/substr,replacement)

在字串中用一些字元替換另一些字元,或替換一個與正則匹配的`字串

var str = "I is Allen.";ace("is","am"); // "I am Allen."

5. slice(start,end)

返回字串的片段

var str = "I am Jack.";e(3,7); // "m Ja"e(3); // "m Jack."e(3,-3); // "m Ja"

6. split(separator,limit)

將一個字串分割為子串,然後將結果作為字串陣列返回

var str = "hello world";t(" "); // ["hello","world"]t(" ",1); // ["hello"]

7. substr(start,lenght)

返回一個從指定位置開始的指定長度的字串

var str = "how do you do";tr(4,2); // "do"tr(4); // "do you do"tr(4,0); // " "tr(4,-1); // " "tr(-3); // "do"

8. substring(start,end)

返回位於 string 物件中指定位置的字串,包含 start 處字元,但不包含 end 處字元

var str = "how do you do";tring(0,3); // "how"

9. toLowerCase()

把字串轉換為小寫

10. toUpperCase()

把字串轉換為大寫

var str = "How do you do";werCase(); // "how do you do"perCase(); // "HOW DO YOU DO"