JavaScript字串截取方法

來源:互聯網
上載者:User

標籤:pre   replace   indexof   div   art   ast   javascrip   slice   檢索   

1 字元方法

  charAt():接收一個參數,返回給定位置的字元 

  charCodeAt():接收一個參數,返回指定位置字元的字元編碼

var str = "hello world";alert(str.charAt(1));    //ealert(str.chatCodeAt(1))    //101

  

2 字串操作方法

  concat():將一個或多個字串拼接起來。   string.concat(str)

  slice(start,[stop]):接收一個或兩個參數,起始位置和結束位置,返回被操作字串的一個子字串

  substr(start,n):接收兩個參數,起始位置和返回字元的個數,返回被操作字串的一個子字串

  substring(start,[stop]):接收一個或兩個參數,起始位置和結束位置,返回被操作字串的一個子字串

  後三個方法傳入參數為負值時表現各不相同,slice()會將傳入的負值與字串長度相加,substr()將第一個負參數加上字串床都,第二個負參數轉換為0,substring()會將負參數都轉換為0。

var str = "hello world"alert(str.slice(-3));//rldalert(str.substring(-3));//hello worldalert(str.substr(-3));//rldalert(str.slice(3,-4));//lowalert(str.substring(3,-4));//helalert(str.substr(3,-4));//""(Null 字元串)

 

3 字串位置方法

  indexOf()  lastIndexOf()    接受兩個參數,要搜尋的字元和起始位置

  string.indexOf(‘a’)  //a表示要搜尋的字元

4 trim() 方法

 

  該方法會建立一個字串的副本,刪除前置和尾碼的所有空格,並返回結果

5 字串大小寫轉換方法

  toLowerCase()

  toLocaleLowerCase()

  toUpperCase()

  toLocaleUpperCase()

 

6 字串 的匹配方法

  match():找到一個或多個Regex的匹配,返回一個數組  string.match(Regex)

  search():替換與Regex匹配的子串,返回字串中第一個匹配的索引值,如果沒有返回-1  string.search(Regex)

  replace():’檢索與Regex相匹配的值,  string.replace(Regex,替換後的字元)

 

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.