zoukankan      html  css  js  c++  java
  • 重复输出字符串

    重复输出字符串

    在 String 对象上定义一个 repeatify 函数。这个函数接受一个整数参数,来明确字符串需要重复几次。这个函数要求字符串重复指定的次数。

    所有的字符串的方法应该添加到String函数构造出的所有字符串上,首先应当判断是否已经有了这个方法,没有则添加。

    最简单的循环方式:

    String.prototype.repeatify = String.prototype.repeatify || function(times){
        var str = '';
        //判断是否正整数
        if(times>0){
            if(typeof times === 'number'&&times%1 === 0){
                for (var i = 0; i < times; i++) {
                    str += this;
                }
                return str;
            }
        }else{
            console.log('参数错误')
            return '';
        }
    };
    console.log('asd'.repeatify(3.1))
    

    ,以下省略判断正整数,

    while循环模式:

    String.prototype.repeatify = String.prototype.repeatify || function(times){
        var str = '';
        while (time>0) {
            str += this;
            time--;
        }
        return str;
    };
    console.log('asd'.repeatify(3))//'asdasdasd'    
    

    拼接字符串可以使用join方法

            var str = [];
            while (times>0) {
                str.push(this);
                times--;
            }
            return str.join('');
    

    递归也可以达到效果:

    String.prototype.repeatify = String.prototype.repeatify || function(times){
        var str = '';
        if(times === 0){
            console.log
            return ''
        }else{
            if(times === 1){
                return this ;
            }else{
                return this + this.repeatify(times-1)
            }
        }
    };
    console.log('asd'.repeatify(3))//'asdasdasd'  
    

    es6中String有repeat方法,接受一个[0,无穷)的整数

    String.prototype.repeatify = String.prototype.repeatify || function(times){
       return times>0?this.repeat(times):''
    };
    console.log('asd'.repeatify(3))//'asdasdasd' 
    

    使用array的fill方法也可以(省略判断)

     return new Array(times).fill(this).join('');  
    

    或者直接join数组

     return new Array(times+1).join(this);
  • 相关阅读:
    在Android中如何获取视频的第一帧图片并显示在一个ImageView中
    利用MsChart控件绘制多曲线图表 z
    国外成熟的程序交易系统的思路 z
    稳健获利
    用vmware安装gho文件
    数学之美 zt
    大型邮箱smtp服务器及端口 收集
    英语之路 zt
    C# Get Desktop Screenshot ZZ
    C#/PHP Compatible Encryption (AES256) ZZ
  • 原文地址:https://www.cnblogs.com/mydia/p/6675767.html
Copyright © 2011-2022 走看看