javaScript字串方法

來源:互聯網
上載者:User

標籤:com   dex   indexof   index   括弧   方法   決定   編碼   範圍   

str=‘我是一串字串‘

charAt()    //擷取一整串字串其中的某一個子字串

取值範圍:0~字串長度-1

alert ( str.charAt() )   //我   括弧裡面什麼都不寫時預設是第0個

alert ( str.charAt(4) )  //符

alert ( str.sharAt(10) )  //空的  括弧裡的值超過字串長度,什麼都彈不出來

alert ( str.sharAt(-2) )  //空的 同上,都超過了字串的合法長度,什麼都彈不出來

---------------------------

sharCodeAt()  //擷取字元相應的 Unicode 編碼    0~9  48~57  /  A~Z  65~90  / a~z  97~122  

取值範圍:0~字串長度-1

alert ( str.sharCodeAt() )  //25105 ‘我’的Unicode編碼  括弧裡面什麼都不寫時預設是第0個

alert ( str.sharCodeAt(12) )         alert ( str.sharCodeAt(-5) )       //空

---------------------------

String.fromCharCode()    //根據字元集編碼返回相應字串

alert ( string.fromCharCode(25105) )  //  ‘我‘

alert ( string.fromCharCode(25105,26153) )   //  ‘我是‘  

alert ( string.fromCharCode(25105,26153).length )  // 2 

---------------------------

indexOf()  // 根據字串的子字元找到對應下標    從左往右找

alert ( str.indexOf(‘一‘) )   // 2  字元數str裡的子字元 ‘一‘ 的下標是2

alert ( str.indexOf(‘串‘,4) )   // 6  第二個參數決定從第幾位開始找,從第四位 ‘ 字‘ 開始找到的 ‘串‘ 的下標是6

alert (str.indexOf(‘串‘,-5) )   // 3   第二個值為負數時預設從第0個開始找

alert ( str.indexOf(‘一串‘) )   // 2  把 ‘一串‘ 當做一個單位,他們第一次出現的位置的下標是2

alert ( str.indexOf(‘我‘,20) )  // -1   超出字串長度,返回-1 

alert ( str.indexOf(‘分‘) )   // -1  字元不存在,沒找到 

---------------------------

lastIndexOf()  // 根據字串的子字元找到對應下標    從右往左找

alert ( str.lastIndexOf(‘我‘) )  // 0  

alert( str.laseIndexOf(‘串‘) )  // 6

alert( str.last.IndexOf(‘串‘,5) )  // 3 從第5位開始從右往左找

javaScript字串方法

聯繫我們

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