🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 字符串常用的方法 ### 1.1 length //获取字符串的长度 ~~~ var a = "hello world"; alert(a.length) //11 ~~~ ### 增加1.2 concat() 方法用于连接两个或多个字符串。 ~~~ var a = "hello world"; var b = "good"; var c = a.concat(b); console.log(c); ~~~ ### 查询1.3 charAt(index) //获取在指定下标的字符 ~~~ var a = "hello world"; alert(a.charAt(0)) // ~~~ ### 1.4 indexOf(value) 检索字符串出现的位置 ~~~ var a = "hello world"; var b = a.indexOf(“h”); //0; var c = a.indexOf(“a”); //-1如果没有返回-1 ~~~ ### 1.5 stringObject.slice(start,end) ~~~ var a = "hello world"; var b = a.slice(0,2); console.log(b); //”he” ~~~ ### 1.6 stringObject.substr(start,length) ~~~ var a = "hello world"; var b = a.substr(0,5); console.log(b); //hello ~~~ ### 1.7 stringObject.substring(start,stop) ~~~ var a = "hello world"; var b = a.substring(0,2); console.log(b); //”he” ~~~ ### 1.8 split() 方法用于把一个字符串分割成字符串数组。 ~~~ stringObject.split(separator,howmany) var a = "hello world"; var b = a.split(""); console.log(b); ~~~ ### 1.9 str.search() //返回下标 ~~~ var a = "hello world"; console.log(a.search("h")); ~~~ ### 1.10 match()返回数组 ~~~ var a ="hello"; console.log(a.match("l")); ~~~ ### 1.11 replace()替换 ~~~ var a ="hello"; var b = a.replace("l","*"); console.log(b); ~~~