js基礎複習---字串操作

來源:互聯網
上載者:User

標籤:

    var str="hello world";
// 1 charAt() 返回字串的第幾個下標字元
console.log(str.charAt(4));
// 2:fixed() 以打字機字型顯示字串
document.write(str.fixed());
// 3:indexOf()返回某個字元或字串片段在字串中首次出現的位置下標,不存在則會返回-1
console.log(str.indexOf("w"));
console.log(str.indexOf("pp"))
// 4:把字串轉化為大小寫
console.log(str.toUpperCase())//大寫
console.log(str.toLowerCase())//小寫
// 5:substring(star,stop) 方法用於提取字串中介於兩個指定下標之間的字元。
// 一個新的字串,該字串值包含 stringObject 的一個子字串,其內容是從 start 處到 stop-1 處的所有字元,其長度為 stop 減 start
// 。
console.log(str.substring(4))//從第4個下標開始,到字串最結尾
console.log(str.substring(2,5))//返回下標2到下標4之間字元
// 6: 把字串顯示為上 下標
document.write(str.sup())//上標
document.write(str.sub())//下標
// 7:將字串顯示為刪除線
document.write(str.strike());
// 8:split, 將字串分割成數組
console.log(str.split(" "));
// ["hello", "world"]
// 9 slice(star,end),提取字串片段
// 包括字串 stringObject 從 start 開始(包括 start)到 end 結束(不包括 end)為止的所有字元。star 允許取負數,取負數則為從末尾開始算
document.write(str.slice(3,6))
// 10 replace(a,b) a為匹配字串的正則規則, b為需要替換為的字串
console.log(str.replace(/hello/,"fuckb"));

js基礎複習---字串操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.