字符串

您所在的位置:网站首页 js的split方法 字符串

字符串

#字符串| 来源: 网络整理| 查看: 265

增加1.1 concat 拼接查询1.2 str[i]1.3 indexOf(value) 根据值查找对应的下标1.4 slice(startIndex,endIndex)1.5 includes 是否包含某位(多位)字符1.6 substring (startIndex,endIndex)1.7 substr(index,length) 截取从某个下标开始的之后的某个长度1.8 charAt(index) 根据下标查找对应的值1.9 search 根据值查找对应的下标 找不到返回-11.10 match 返回匹配的字符串其它1.11 replace()替换字符串1.12 split()将字符串分割成字符串数组1.13 trim( ) 去除字符串前后的空格1.14 startsWith( ) 以…开头的字符串

增加

1.1 concat 拼接var a = 'hello'var b = "world"console.log(a.concat(b));

查询

1.2 str[i]var str="hello"console.log(str[0]);//h

1.3 indexOf(value) 根据值查找对应的下标var str="hello"console.log(str.indexOf("o")) //输出某个字符对应的下标值

1.4 slice(startIndex,endIndex)var arr = "hello"console.log(arr.slice(0)); // hello 截取从某个下标开始console.log(arr.slice(0,3)); // hel

1.5 includes 是否包含某位(多位)字符var arr = "hello"console.log(arr.includes("eh")); //false

1.6 substring (startIndex,endIndex)var str="hello"console.log(str.substring(0,3))

1.7 substr(index,length) 截取从某个下标开始的之后的某个长度var str="hello"console.log(str.substr(0,2)) // he

1.8 charAt(index) 根据下标查找对应的值var str="hello"console.log(str.charAt(1)) //输出某一个下标值的字符

1.9 search 根据值查找对应的下标 找不到返回-1var str = "helloworld";var res = str.search("r");console.log(res)console.log(str.search("a"))

1.10 match 返回匹配的字符串 /* match返回的是一个数组,将匹配的字符返回一个数组 */ /* 1、存在 数组 2、不存在 null */ var str ="你好你来你的"; var res = str.match("你"); /* ["你"] */ console.log(res) console.log(str.match("他"))

其它

1.11 replace()替换字符串var str = "hello"console.log(str.replace("l","*")); // he*lo

1.12 split()将字符串分割成字符串数组var str = "hello"console.log(str.split()); // 将字符串转为数组 ["hello"]console.log(str.split("")); // ["h","e","l","l","o"]console.log(str.split("e"));// ["h","llo"]

1.13 trim( ) 去除字符串前后的空格// 正则表达式: /^\s+|\s+$/g var str = " hello ";var arr = []arr.push(str.trim())console.log(arr)

1.14 startsWith( ) 以…开头的字符串// startsWith() 以...开头的字符串// endsWith()var str = "武汉"console.log(str.startsWith("武")) // trueconsole.log(str.endsWith("汉"))


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3