JS字符串⼩结字符串⽅法有哪些声明字符串字符串遍历字符串
⽅法indexofaharAtsp。。。
JS字符串⼩结
声明
var  变量名 = '  '
转义字符
\ n  换⾏
\t  缩进
\\  制表符
\"  双引号
\'  单引号
console.log('a \n a')
console.log('a \t a')
console.log('\\')
console.log('\"')
console.log('\'')
======结果为=======
a
a
a  a
\
"
'
元素遍历
for in
字符串也可⽤ for in 遍历  结果为 0 到 字符串长度-1 的数字
var x = 'aaaaaa'
for( var i in x ){
console.log(i)
}
========结果为======
1
2
3
4
5
for of
同样的 字符串也可以⽤ for  of 进⾏遍历  结果为 字符串中的每⼀个字符
var x = 'abcdefg'
for( var i of x ){
console.log(i)
}
======结果为======
a
b
c
d
e
f
g
⽅法
1.  变量名.indexOf( '字符 ')    获得元素⾸次出现的索引  若没有 返回-1
var x = 'abcdecfg'
console.log(x.indexOf('c') )
console.log(x.indexOf('z') )
js 正则替换========结果为======
2
-1
2. 变量名.lastIndexOf(' 字符 ')    获得元素最后⼀次出现的索引
var x = 'abcdecfg'
console.log(x.lastIndexOf('c') )
console.log(x.lastIndexOf('z') )
============结果为=========
5
-1
3. 变量名.charAt(数字)  索引访问 如果长度不够 返回空
var x = 'abcdecfg'
console.log(x.charAt(3) )
console.log(x.charAt(99) )
=======结果为=======
d
//此⾏有⼀个空格
4. 变量名.charCodeAt(数字)  获得 字符串中 索引为  数字  的的ASCLL码值
var x = 'abcdecfg'
console.log(x.charCodeAt(0))
console.log(x.charCodeAt(2))
==========结果为=========
97      // 字符a 的ASCLL码为 97
99      // 字符c 的ASCLL码为 99
5. String.fromCharCode(数字)  以数字 作为ASCLL码 获得字符
console.log(String.fromCharCode(97))
console.log(String.fromCharCode(99))
==========结果为============
a
c
6. 变量名.toLowerCase( )    转换为纯⼩写
var x = 'aBcDeFg'
console.LowerCase())
=========结果为=========
abcdefg
7. 变量名.toUpperCase( )    转换为纯⼤写
var x = 'aBcDeFg'
console.UpperCase())
=========结果为===========
ABCDEFG
8. 变量名.split( '字符' )    以字符分割字符串    返回分割完成后的数组  (引号内也可以什么都不填)
var x = 'd.e.f.g'
console.log(x.split('.'))
==========结果为========
(7) ["a", "b", "c", "d", "e", "f", "g"]
9. 变量名.substr(数字1,数字2)  分割返回  从索引为数字1的位置 取数字2 个字符 (包括索引为 数字1 的字符)
var x = 'abcdefg'
console.log(x.substr(2,3))
======结果为========
cde
10. 变量名.subString(数字1,数字2 )  分割  返回索引为数字1 到索引为数字2之间的字符  (不包括索引为数字2的字符,但是包括数字1)
var x = 'abcdefg'
console.log(x.substring(2,4))
========结果为========
cd
11. 变量名.replace( '字符1','字符2')  返回  将所有的 字符1  替换为 字符2
var x = 'abcdefg'
console.place('b','z'))
===========结果为============
azcdefg
12. 变量名.repeat( 数字 )    重复  数字  次
console.peat(3))
============结果为========
abcdefgabcdefgabcdefg
13. 变量名.trim()/  变量名.trimLeft( ) / 变量名.trimRight()    去除空格  去除左侧空格  去除右侧空格
var x = '  abcdefg  '
console.imLeft())
console.imRight())
console.im())
=======结果为======
abcdefg        //此⾏后有两个空格
abcdefg
abcdefg
14. 变量名.match(正则或其他表达式)  匹配字符 或者 第⼀个能匹配到正则的字符
var x = 'ab1cdefg'
var z = 'c'
var y = /\d/
console.log(x.match(z))
console.log(x.match(y))
=======结果为=====
["c", index: 3, input: "ab1cdefg"]
["1", index: 2, input: "ab1cdefg"]
15. 变量名.big()  在字符串外包⼀层 big 标签      ⼀般输出到 html中
16. 变量名.small()  在字符串外包⼀层 small 标签      ⼀般输出到 html中
17. 变量名.bold()  在字符串外包⼀层 bold 标签        ⼀般输出到 html中
18. 变量名.fontcolor( 颜⾊值 )  在字符串外包⼀层 fontcolor 标签      ⼀般输出到 html中
19. 变量名.fontsize( 数字 )  在字符串外包⼀层 fontsize 标签      ⼀般输出到 html中
20. 变量名.link( 链接 )  在字符串外包⼀层 a 标签      ⼀般输出到 html中
console.log(x.big())
console.log(x.small())
console.log(x.bold())
console.log(x.fontcolor('#ff0000'))
console.log(x.fontsize(20))
console.log(x.link('www.baidu'))
document.write(x.big()+'<br>')
document.write(x.small()+'<br>')
document.write(x.bold()+'<br>')
document.write(x.fontcolor('#ff0000')+'<br>')
document.write(x.fontsize(20)+'<br>')
document.write(x.link('www.baidu'))
=======结果为==========
<big>ab1cdefg</big>
<small>ab1cdefg</small>
<b>ab1cdefg</b>
<font color="#ff0000">ab1cdefg</font>
<font size="20">ab1cdefg</font>
<a href="www.baidu">ab1cdefg</a>
21. 变量名.sub  返回 变量的下标
22. 变量名.sup  返回 变量的上标
var x = 'ab1cdefg'
document.write(x,x.sub(),x.sup())
23. 变量名.startsWith( ' 字符  ') 返回布尔 判断字符串是否以  字符开头
var x = 'abcdefg'
console.log(x.startsWith('a'))
console.log(x.startsWith('b'))
--------结果为--------
true
false
24. 变量名.endsWith( ' 字符  ') 返回布尔 判断字符串是否以  字符结尾
var x = 'abcdefg'
console.dsWith('g'))
console.dsWith('z'))
----------结果为------
true
false