string.js 858 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. module.exports = {
  2. // 字符串长度计算
  3. count : function(str, countSpace = true){
  4. if(countSpace){return str.length;}
  5. return this.removeAllSpace(str).length;
  6. },
  7. // 去除全部空格
  8. removeAllSpace : function(str){
  9. return str.replace(/\s+/g, "");
  10. },
  11. // 去除首尾空格
  12. trim : function(str){
  13. return str.trim();
  14. },
  15. // 去除左侧空格
  16. trimL : function(str){
  17. return str.replace(/^\s+/g, "");
  18. },
  19. // 去除右侧空格
  20. trimR : function(str){
  21. return str.replace(/\s+$/g, "");
  22. },
  23. // 字符串搜索
  24. search : function (str, kwd, caseSensitive = true) {
  25. if(!caseSensitive){
  26. kwd = kwd.toLowerCase();
  27. str = str.toLowerCase();
  28. }
  29. return str.indexOf(kwd);
  30. },
  31. // 获取 扩展名
  32. getExtension : function (str) {
  33. str = str.split('.');
  34. return str.pop();
  35. }
  36. }