String的使用

來源:互聯網
上載者:User

標籤:style   http   java   使用   width   javascript   

                                String的常用方法

方法

說明

Anchor()

建立html錨

Concat()

把字串串連起來

indexOf()

尋找字元出現的位置

lastIndexOf()

尋找最後出現字元

charAt()

返回指定位置的字元

Substring()

截取字串

Substr()

截取字串

Spilt()

分割字串

toLowerCase()

把字串轉換成小寫字母

toUpperCase()

把字串轉換成大寫字母

Sub()

把字串顯示為下標

Sup()

把字串顯示為上標

 

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script type="text/javascript" language="javascript">
 /************建立字串對象***********/
 //建立字串對象(方法一)
 //var str = "hello world";
 //建立字串對象(方法一)
 //var str = new String("hello world");
 
 /***********字串對象的常用方法***************/
 var str = "hello world";
 //indexOf用來尋找指定子字串的第一出現的索引位置,找不到就返回-1
 //document.write(str.indexOf("o",0));//輸出4
 //document.write(str.indexOf("o",5));//輸出7
 //lastIndexOf用來尋找指定子字串出現的最後位置,找不到就返回-1(注意:本方法是從字串的最後向前面進行尋找,第二個參數代表開始尋找的索引位置)
 //document.write(str.lastIndexOf(‘o‘,str.length));
 /*********substring與substr*********************/

var str = "hello world";
 //參數一:開始截取的下標  參數二:截取到第幾個字元
 document.write(str.substring(1,3));//輸出el
 document.write("<br>");
 //參數一:開始截取的下標  參數二:截取多少個字元
 document.write(str.substr(1,3));
 document.write("<br>");
 document.write(str.length);//輸出字串的長度

</script>

</head>

<body>

</body>

</html>

聯繫我們

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