Javascript學習教程:字串串連

來源:互聯網
上載者:User

文章簡介:JS內建功能之一:字串串連.

JS內建功能之一:字串串連;

如果將加號(+)運算子用於數字,表示兩數相加;

但將它作用於字串,則表示字串相連,將第二個字串串連在第一個之後。

var a = "Hello,"; var b = "world";

var s = a + b; //產生字串“Hello,world”;

alert(s);//顯示出字串

alert(s.length);//顯示字串的長度

字串其他的調用方法:

s.charAt(0); //第一個字元:“H”

s.charAt(s.length-1); //最後一個字串“d”

s.substring(1,4); //第2~4個字串注意不是第2~5個字串 “ell”;

s.slice(1,4); // 同上 “ell”;

s.replace("H","h"); //“h”替換“H”,全文字元替換,

s.toUpperCase(); //全文大寫

s.slice(-3); //最後三個字元“rld”;

s.indexOf("l"); //字元l首次出現的位置; “2”

s.lastIndexOf("l"); //字元l最後出現的位置; “10”

s.indexOf("l",3); //在位置33及之後首次出現字串了的位置 “3”

s.split(", "); //[“hello”,“world”]分割成子串

註:字串不能相減,只能截取部分字串相加“+”;



聯繫我們

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