翼度科技»论坛 编程开发 JavaScript 查看内容

3 JavaScript字符串操作

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
3 字符串操作

常用的字符串操作相关的方法:
  1. s.split()  字符串切割
  2. s.substr(start, len)  字符串切割, 从start开始切, 切len个字符
  3. s.substring(start, end)  字符串切割, 从start切割到end
  4. s.length  字符串长度
  5. s.charAt(i) 第i索引位置的字符  s[i]
  6. s.indexOf('xxx')  返回xxx的索引位置, 如果没有xxx. 则返回-1
  7. s.lastIndexOf("xxx") 返回xxx的最后一次出现的索引位置,如果没有xxx. 则返回-1
  8. s.toUpperCase() 转换成大写字母
  9. s.startsWith("xxx")  判断是否以xxx开头
  10. s.charCodeAt(i) 某个位置的字符的ascii
  11. String.fromCharCode(ascii) 给出ascii 还原成正常字符
复制代码
关于null和undefined. 这两个会很容易混. 可以这样来记. null就是空对象. undefined就是空变量. 两者都可以表示空. 啥也没有. 本质其实是一样的. 都啥也干不了. 两者都可以当做false来看待就好了.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>字符串操作</title>
  6. </head>
  7. <body>
  8.    
  9. </body>
  10. </html>
复制代码
  1. // 字符串操作
  2. s = 'hellobadyjavascripts';
  3. // 字符串切割
  4. console.log(s.split('a'));   // ['hellob', 'dyj', 'v', 'scripts']
  5. // 从start开始切, 切len个字符
  6. console.log(s.substr(1, 5)); // ellob
  7. // 从start切割到end
  8. console.log(s.substring(1, 5));  // ello
  9. // 字符串长度
  10. console.log(s.length);  // 20
  11. // 第i索引位置的字符
  12. console.log(s.charAt(5));   // b
  13. // 返回xxx的索引位置, 如果没有xxx. 则返回-1
  14. console.log(s.indexOf('c'));    // 14
  15. console.log(s.indexOf('x'));    // -1
  16. // 返回xxx的最后一次出现的索引位置,如果没有xxx. 则返回-1
  17. console.log(s.lastIndexOf('a'));    // 12
  18. // 转换成大写字母
  19. console.log(s.toUpperCase());   // HELLOBADYJAVASCRIPTS
  20. // 转换成小写字母
  21. console.log(s.toLowerCase());   // hellobadyjavascripts
  22. // 判断是否以xxx开头
  23. console.log(s.startsWith('h'));     // true
  24. console.log(s.startsWith('q'));     // false
  25. // 某个位置的字符的ascii
  26. console.log(s.charCodeAt(6));   // 97
  27. // 给出ascii 还原成正常字符
  28. console.log(String.fromCharCode('98'));     // b
复制代码
代码的效果图如下:



来源:https://www.cnblogs.com/zczhaod/p/17639583.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具