1. 程式人生 > >JS中String物件常用方法

JS中String物件常用方法

1.chatAt():返回在指定位置的字元
用法:str.charAt(index) str為字串、index為索引

2.charCodeAt():返回在指定位置的字元的Unicode編碼
用法:str.charCodeAt(index):str為字串、index為索引

3.concat():連線字串
用法:concat(str1,str2,…):str1…為需要連線的各個字串

4.indexOf():檢索某個字元在字串中的位置
用法:indexOf(s,index):s為要檢索的字元、index為開始檢索的索引值,索引值可選

5.match():在字串內檢索指定的值,或找到一個或多個正則表示式的匹配
用法:str.match(s):str為字串、s為要檢索的字元或者字串

6.substr():提取從索引值開始檢索指定數目的字元
用法:str.substr(index,length):index為索引值、length為檢索的數目

7.substring():提取從索引值開始檢索到指定位置的字元
用法:str.substring(index1,index2):index1和index2為兩個索引值

8.slice():提取指定位置到指定位置的字元
用法:str.slice(index1,index2):index1,index2分別為兩個索引值,index1如果沒有則預設從頭開始,與substring相比,slice更加靈活,其索引值可以是負數

9.toString():返回字串
用法:str.toString()

10.split():將字串分割為字元陣列
用法:str.split(s,number):s為開始分割的標誌、number為分割之後保留的個數

11.toLowerCase():將字串轉化為小寫
用法:str.toLowerCase()

12.toUpperCase():將字串轉換為大寫
用法:str.toUpperCase()