06-js字符串

1. 转义字符
\'      '
\n      换行
\t      制表符
\\      \
\x##    表示ASCII
\u####  表示一个Unicode字符

案例1:

<script>
    var str = 'I\'m a 帅哥';
    console.log(str);
</script>

案例2:

<script>
    var str = 'hello world \n hello 如来';
    console.log(str);
</script>
2. 表示多行字符串
`
多行字符串
`

案例1:

<script>
    var str = `hello
               world`;
    console.log(str);
</script>
3. 字符串的拼接
用 + 把多个字符串连接起来

案例1:

'I' + 'love' + 'You'
4. 操作字符串
4.1 计算字符串长度
<script>
    var str = 'hello world';
    console.log(str.length);
</script>
4.2 访问字符串某个字符
<script>
    var str = 'hello world';
    console.log(str[0]);
</script>
4.3 字符串字母全部变为大写
<script>
    var str = 'hello world';
    console.log(str.toUpperCase());
</script>
4.4 字符串字母全部变为小写
<script>
    var str = 'Hello World';
    console.log(str.toLowerCase());
</script>
4.5 索引字符或字符串在字符串出现的首位置
<script>
    var str = 'hello world';
    console.log(str.indexOf('o'));
</script>
4.6 返回指定索引区间的子串
<script>
    var str = 'hello world';
    console.log(str.substring(1, 3));
</script>

猜你喜欢

转载自blog.csdn.net/rulaixiong/article/details/80665861