字符串的简单运用

原生js实现字符串去除两边的空格

var Str = " qwert    ",
function Remmovespace(str){
var str1 = Str.replace(/^\s*|\s$/g,"")
}
Remmovespace(Str);
结果: qwert

这个题目不是很明白:

var num = 1;
      var obj = {
        num: 2,
        fn: (function() {
          this.num *= 2;
          num += 3;
          var num = 1;
          return function() {
            num += 2;
            this.num += 2;
            console.log(++num, "xxx");
          };
        })()
      };
      var f = obj.fn;
      f();
      obj.fn();
      console.log(window.num, obj.num);

原生js字符串去重:

var str = "aahhgggsssjjj";
      var str1 = str.split("");
      for (var i = 0; i < str1.length; i++) {
        var arr = [];
        if (arr.indexOf(str1[i] == -1)) {
          arr.push(str1[i]);
        }
        console.log(arr.join(""));
      }

统计字符串出现的次数:

 var str = "aahhgggsssjjj";
      var str1 = str.split("");
      var arr = [];
      var count = 0;
      var sum = [];
      for (var i = 0; i < str1.length; i++) {
        if (arr.indexOf(str1[i]) == -1) {
          arr.push(str1[i]);
        }
      }

      for (var j = 0; j < arr.length; j++) {
        console.log(arr[j]);
        for (var i = 0; i < str1.length; i++) {
          console.log(str1[i]);
          if (arr[j] == str1[i]) {
            count++;
          }
        }
        sum.push(count);
        count = 0;
      }
      console.log(arr);
      for (var i = 0; i < arr.length; i++) {
        var str3 = sum[i] % 2 == 0 ? "偶数" : "奇数";
        console.log(arr[i] + "出现了" + sum[i] + "次");
        console.log(arr[i] + "出现了" + str3 + "次");
      }

你可能感兴趣的:(字符串的简单运用)