创建字符串和字符串的常用方法

1.创建字符串有两种方式:第一种方式:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // new String类型的对象
        let str1 = new String('Hello')
        console.log(str1);
    </script>
</body>

</html>

第二种方式:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         // 第二种方式:直接定义
         let str = 'HelloWorld'
        console.log(str);
    </script>
</body>

</html>

2.charAt() 返回在指定位置的字符 注意:位置从0开始

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         console.log(str.charAt(2));
    </script>
</body>

</html>

3.concat() 连接两个或更多字符串,并返回新的字符串,也可以直接用"+"拼接

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         let str1="你好"
         console.log(str.concat(str1));
         console.log(str+str1);
    </script>
</body>

</html>

4.indexOf() 返回某个指定的字符串值在字符串中首次出现的位置。如果找不到返回-1

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         console.log(str.indexOf('o'));
        console.log(str.indexOf('x'));
    </script>
</body>

</html>

5. lastIndexOf() 从后向前搜索字符串,位置还是从前往后数。如果找不到返回-1

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         console.log(str.lastIndexOf('o'));
        console.log(str.lastIndexOf('x'));
    </script>
</body>

</html>

6.includes() 查找字符串中是否包含指定的字符串,包含返回true,不包含返回false:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         console.log(str.includes('ll'));
        console.log(str.includes('cb'))
    </script>
</body>

</html>

7.slice() 提取字符串的片断,并在新的字符串中返回被提取的部分

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
          // 注意:能取到3位置,但是取不到6位置。
         console.log(str.slice(3,6));
        //  可以传负数,表示从后往前数位置
         console.log(str.slice(-6,-3));
        //  只传一个参数,表示从起始位往后全部截取
        console.log(str.slice(3));

    </script>
</body>

</html>

8.substring() 提取字符串中两个指定的索引号之间的字符

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         console.log(str.substring(3,6));

    </script>
</body>

</html>

9.substr() 从起始索引号提取字符串中指定数目的字符

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = 'HelloWorld'
         console.log(str.substr(3,6));
    </script>
</body>

</html>

10.split() 把字符串分割为字符串数组

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = "张家辉,刘青云,古天乐"
        // 根据,号 分割字符串,并返回一个数组
        console.log(str.split(','));
    </script>
</body>

</html>

11.toLowerCase() 把字符串转换为小写

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = "HelloWorld"
         console.log(str.toLowerCase());
    </script>
</body>

</html>

12. toUpperCase() 把字符串转换为大写

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = "HelloWorld"
         console.log(str.toUpperCase());
    </script>
</body>

</html>

13.trim() 去除字符串两边的空白

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let str = "  HelloWorld  "
        //  原来str字符串的长度
         console.log(str.length);
        //  去除两端空格后的长度
         console.log(str.trim().length);
    </script>
</body>

</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中字符串常用方法包括: 1. `capitalize()`: 把字符串的第一个字符大写。 2. `casefold()`: 把字符串所有字符转换为小写。 3. `center(width[, fillchar])`: 返回一个指定宽度的居中字符串,可指定填充字符。 4. `count(sub[, start[, end]])`: 返回指定子字符串字符串中出现的次数。 5. `encode([encoding[, errors]])`: 返回字符串的编码版本。 6. `endswith(suffix[, start[, end]])`: 检查字符串是否以指定后缀结尾。 7. `expandtabs([tabsize=8])`: 把字符串中的制表符替换为空格,默认大小为8。 8. `find(sub[, start[, end]])`: 返回指定子字符串字符串中第一次出现的索引,不存在返回-1。 9. `format(*args, **kwargs)`: 格式化输出字符串。 10. `index(sub[, start[, end]])`: 返回指定子字符串字符串中第一次出现的索引,不存在抛出异常。 11. `isalnum()`: 检查字符串是否只包含字母和数字。 12. `isalpha()`: 检查字符串是否只包含字母。 13. `isdigit()`: 检查字符串是否只包含数字。 14. `islower()`: 检查字符串是否只包含小写字母。 15. `isnumeric()`: 检查字符串是否只包含数字。 16. `isspace()`: 检查字符串是否只包含空格。 17. `istitle()`: 检查字符串是否为标题化(单词首字母大写)。 18. `isupper()`: 检查字符串是否只包含大写字母。 19. `join(iterable)`: 把可迭代对象中的元素连接成一个字符串。 20. `ljust(width[, fillchar])`: 返回一个指定宽度的左对齐字符串,可指定填充字符。 21. `lower()`: 把字符串所有字符转换为小写。 22. `lstrip([chars])`: 去掉字符串左边的指定字符,默认为空格。 23. `maketrans(x[, y[, z]])`: 创建字符映射表。 24. `partition(sep)`: 把字符串分成三部分,第一部分到分隔符前,第二部分为分隔符,第三部分为分隔符后的部分。 25. `replace(old, new[, count])`: 把字符串中的旧子串替换为子串,可指定替换次数。 26. `rfind(sub[, start[, end]])`: 返回指定子字符串字符串中最后一次出现的索引,不存在返回-1。 27. `rindex(sub[, start[, end]])`: 返回指定子字符串字符串中最后一次出现的索引,不存在抛出异常。 28. `rjust(width[, fillchar])`: 返回一个指定宽度的右对齐字符串,可指定填充字符。 29. `rpartition(sep)`: 把字符串从右边开始分成三部分,第一部分到分隔符前,第二部分为分隔符,第三部分为分隔符后的部分。 30. `rsplit([sep[, maxsplit]])`: 把字符串从右边开始分割,返回一个列表。 31. `rstrip([chars])`: 去掉字符串右边的指定字符,默认为空格。 32. `split([sep[, maxsplit]])`: 把字符串分割,返回一个列表。 33. `splitlines([keepends])`: 按行分割字符串,返回一个列表。 34. `startswith(prefix[, start[, end]])`: 检查字符串是否以指定前缀开头。 35. `strip([chars])`: 去掉字符串两边的指定字符,默认为空格。 36. `swapcase()`: 把字符串中大写字母转换为小写字母,小写字母转换为大写字母。 37. `title()`: 把字符串单词的第一个字母大写。 38. `translate(table[, deletechars])`: 根据映射表转换字符串。 39. `upper()`: 把字符串中所有字符转换为大写。 40. `zfill(width)`: 在字符串左边填充0,直到达到指定宽度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值