String對象常用API

concat(str1,str2,···)

連接字符串

indexOf(str,start)

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

var str = "hello world";
str.indexOf("hello");  // 0
str.indexOf("o",5);  // 7
str.indexOf("World");  // -1

lastIndexOf(str,start)

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

var str = "hello world";
str.lastIndexOf("hello");  // 0
str.lastIndexOf("o",3);  // -1
str.lastIndexOf("o",5);  // 4

replace(regexp/substr,replacement)

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

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

slice(start,end)

返回字符串的片段

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

split(separator,limit)

將一個字符串分割爲子串,然後將結果作爲字符串數組返回

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

substr(start,lenght)

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

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

substring(start,end)

返回位於 string 對象中指定位置的字串,包含 start 處字符,但不包含 end 處字符

var str = "how do you do?";
str.substring(0,3);  // "how"

toLowerCase()

把字符串轉換爲小寫

toUpperCase()

把字符串轉換爲大寫

var str = "How do you do?";
str.toLowerCase();  // "how do you do?"
str.toUpperCase();  // "HOW DO YOU DO?"


PS:博客搬家了,以後不再 CSDN 更新了,見諒。最新博客地址:http://www.cnblogs.com/yjzhu/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章