💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 字符串常用的方法 ### 1.1 length //获取字符串的长度 ~~~ var a = "hello world"; alert(a.length) //11 ~~~ ### 1.2 concat()连接两个或多个字符串 ~~~ concat() 方法用于连接两个或多个字符串。 var a = "hello world"; var b = "good"; var c = a.concat(b); console.log(c); //hello worldgood ~~~ ### 1.3 charAt(index) 获取在指定下标的字符 ~~~ charAt(index) //获取在指定下标的字符 var a = "hello world"; alert(a.charAt(0)) // h ~~~ ### 1.4 indexOf(value) 检索字符串出现的位置 ~~~ indexOf(value) 检索字符串出现的位置 var a = "hello world"; var b = a.indexOf(“h”); //0; var c = a.indexOf(“a”); //-1如果没有返回-1 ~~~ ### 1.5 slice(start,end)分割字符串 ~~~ stringObject.slice(start,end) var a = "hello world"; var b = a.slice(0,2); console.log(b); //”he” ~~~ ### 1.6 substr(start,length) ~~~ stringObject.substr(start,length) var a = "hello world"; var b = a.substr(0,5); console.log(b); //hello ~~~ ### 1.7 substring(start,stop) ~~~ stringObject.substring(start,stop) var a = "hello world"; var b = a.substring(0,2); console.log(b); //”he” ~~~ ### 1.8 split() 方法用于把一个字符串分割成字符串数组 ~~~ split() 方法用于把一个字符串分割成字符串数组。 stringObject.split(separator,howmany) var a = "hello world"; var b = a.split(""); console.log(b); //(11) ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"] ~~~ ### 1.9 str.search() //返回下标 ~~~ var a = "hello world"; console.log(a.search("h")); ~~~ ### 1.10 match()返回数组 ~~~ var a ="hello"; console.log(a.match("l")); //["l", index: 2, input: "hello", groups: undefined] ~~~ ### 1.11 replace()替换 ~~~ var a ="hello"; var b = a.replace("l","*"); console.log(b); ~~~