JavaScript 字符串处理方法总结

来源:互联网 发布:破解音乐付费软件 编辑:程序博客网 时间:2024/05/08 14:27
变量从字符串转换成int和float型 
  1. var weightincrease = "2.5kg";
  2. undefined
  3. parseInt(weightincrease);
  4. 2
  5. parseFloat(weightincrease);
  6. 2.5

字符串处理方法
  1. var words = "鱼神是个帅哥";
  2. undefined
  3. words.length
  4. 6
  5. words.charAt(0);
  6. "鱼"
  7. words.charAt(words.length-1);
  8. "哥"
  1. var words = "鱼神是个神帅哥";
  2. undefined
  3. words.indexOf('神');
  4. 1
  5. words.lastIndexOf('神');
  6. 4
  1. //截图字符串
  2. words.substring(4,6)
  3. "神帅"
  4. //替换字符串
  5. words.replace('是个','无敌')
  6. "鱼神无敌神帅哥"
  7. //按,分割
  8. words= '鱼神无敌,神帅哥'
  9. "鱼神无敌,神帅哥"
  10. words.split(',')
  11. ["鱼神无敌", "神帅哥"]
  12. //按,分割给新对象
  13. var newwords = words.split(',')
  14. undefined
  15. newwords
  16. ["鱼神无敌", "神帅哥"]
  17. typeof(newwords)
  18. "object"
  19. newwords[0]
  20. "鱼神无敌"
  21. newwords[1]
  22. "神帅哥"
0 0