【javascript】字串對象常用API

來源:互聯網
上載者:User
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?" 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.