属性
length: 字符串长度
prototype、constructor
常用方法
indexOf() 返回某个指定的字符串值在字符串中首次出现的位置
'123456789'.indexOf('23') //1
'123456789'.indexOf('235') //-1
concat() 连接两个或更多字符串,并返回新的字符串。
str = "111 "
str1 = "12121 "
str2 = "222223 "
arr = ['a ','bb ','ccc ']
str.concat(str1,str2,...arr) //"111 12121 222223 a bb ccc "
// str 不发生变化
replace() 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串
str = "111 12138 9527 111 3333 555"
str_new = str.replace('111','aaa') //"aaa 12138 9527 111 3333 555"
// str 不发生变化
replaceAll()
str = "111 12138 9527 111 3333 555"
str_new = str.replaceAll('111','aaa') //"aaa 12138 9527 aaa 3333 555"
// str 不发生变化
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分
//string.slice(start,end)
a = '0123456789'
a.slice(1,5) //"1234"
a.slice(2,-1) //"2345678"
a.slice(-5) //"56789"
a.slice(8,5) //""
substring() 提取字符串中介于两个指定下标之间的字符
//string.substring(start,stop)
a = '0123456789'
a.substring(1,5) //"1234"
a.substring(1,1) //""
a.substring(5,1) //=>a.substring(1,5) "1234"
a.substring(5,-1) //=>a.substring(5,0) => a.substring(0,5) "1234"
a.substring(5,2.5) //=>a.substring(5,2) => a.substring(2,5) "234"
a.substring(5,'ss')
// =>a.substring(5,2) =>a.substring(5,0) => a.substring(2,5) "234"
substr() 可在字符串中抽取从 start 下标开始的指定数目的字符
//string.substr(start,length)
a = '0123456789'
a.substr(1,5) //"12345"
a.substr(-4,2) //'67'
a.substr(4,-2) // => a.substr(4,0) ""
a.substr(1.2,5.2) // => a.substr(1,5) "12345"
a.substr('aa','5') // => a.substr(0,5) "01234"
substring()、substr()、slice() 的 区别与相同点
split() 根据指定字符/字符串作为分隔把字符串分割为字符串数组
str = "111,222,333,aaa,9527"
str.split(',') // ["111", "222", "333", "aaa", "9527"]
// str 不发生变化
toLowerCase() //把字符串转换为小写,推荐使用toLocaleLowerCase()
toUpperCase() //把字符串转换为大写,推荐使用toLocaleUpperCase()
toLowerCase和toLocaleLowerCase的区别
trim() 去除字符串两边的空白
trimStart() 去除左边;trimEnd() 去除右边
包装方法 HTML
link() 将字符串显示为链接。
str = "40Test"
str1 =str.link("www.baidu.com") //"<a href=\"www.baidu.com\">40Test</a>"
bold()、big()、bold() …
扩展 Json…
JSON.parse(json) // 将json字符串转换成json对象
JSON.stringify(str) //将json对象转换成json字符串
eval(string) //执行其中的javascript表达式或要执行的语句。
res = "str = ''; for(let i=0;i<5;i++){str+=i+','};console.log(str)"
eval(res) //0,1,2,3,4,
raw() 返回一个斜杠都被转义(即斜杠前面再加一个斜杠)的字符串 参考
整理&其他 菜鸟教程
方法名 | 说明 |
---|---|
indexOf() | 返回某个指定的字符串值在字符串中首次出现的位置 |
concat() | 连接两个或更多字符串,并返回新的字符串 |
replace() | 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串 |
replaceAll() | 在字符串中查找匹配的子串, 并替换所有 |
slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分;str.slice(start,end) |
substring() | str.substring(start,stop) |
substr() | str.substr(start,length) |
split() | 根据指定字符/字符串作为分隔把字符串分割为字符串数组; str.split(’,’) |
toLowerCase() | 把字符串转换为小写 |
toUpperCase() | 把字符串转换为大写 |
trim() | 去除字符串两边的空白 |
trimStart() | 去除左边的空白 |
trimEnd() | 去除右边的空白 |
charAt() | 返回在指定位置的字符 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码 |
fromCharCode() | 将 Unicode 编码转为字符 |
match() | 查找找到一个或多个正则表达式的匹配 |
repeat() | 复制字符串指定次数,并将它们连接在一起返回 |
search() | 查找与正则表达式相匹配的值 |
startsWith() | 查看字符串是否以指定的子字符串开头 |
valueOf() | 返回某个字符串对象的原始值 |
toString() | 返回一个字符串 |