zoukankan      html  css  js  c++  java
  • 字符串处理

    1.传入一个string类型的参数,然后将string的每个字符间加个空格返回

    1    String.prototype.spacify = function(){
    2        return this.split("").join(" ");
    3    };
    4    console.log("abc".spacify()); //a b c

    2.将数组[1,2,3,4,5]变为[1,2,3,4,5,1,2,3,4,5]

    1    Array.prototype.duplicator = function(){
    2        return this.concat(this);7    
    8    console.log([1,2,3,4,5].duplicator()); //[1,2,3,4,5,1,2,3,4,5]

    3.输出参数,且加上规定的前缀

    1    function log(){
    2        var str = Array.prototype.slice.call(arguments);
    3        str.unshift("(app)");
    4        console.log.apply(console,str);
    5    }
    6    log("hello","world");  //(app) hello world

     4.接收一个整数作为字符串重复的次数,返回重复的字符串

    1   String.prototype.repeatify = String.prototype.repeatify || function (num){
    2        var str = "";
    3        for (var i = 0; i < num; i++){
    4            str +=this;
    5        }
    6        return str;
    7    };
    8    console.log("hello".repeatify(3));  //hellohellohello

     5.变为驼峰式

     1   var foo = "get-element-by-id";
     2    function como(str){
     3        var arr = str.split("-");
     4        var elem;
     5        var newArr = [arr[0]];
     6        for(var i =1,len = arr.length; i < len; i++){
     7            elem = arr[i];
     8            elem = elem.charAt(0).toUpperCase()+elem.slice(1,elem.length);
     9            newArr.push(elem);
    10        }
    11        return newArr.join("");
    12    }
    13    console.log(como(foo));  //getElementById
  • 相关阅读:
    000_linux之Ubuntu安装
    001_linux基础命令
    018_linux驱动之_阻塞和非阻塞
    019_linux驱动之_定时器的引入
    017_linux驱动之_信号量
    016_linux驱动之_原子操作
    python logging模块整理
    python sys与shutil模块
    python configparser模块
    python os模块
  • 原文地址:https://www.cnblogs.com/webliu/p/4639517.html
Copyright © 2011-2022 走看看