JS字符串常用方法(自)---3、字符串重复

JS字符串常用方法(自)---3、字符串重复

一、总结

一句话总结:

字符串重复的函数是repeat(),作用是对字符串进行重复,参数是count(重复次数),返回值是成功操作的字符串。
repeat()
作用:对字符串进行重复
参数:count(重复次数)
返回值:重复操作之后的字符串

console.log('abc'.repeat(2));//'abcabc'

二、字符串重复

博客对应课程的视频位置:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>repeat()</title>
 6 </head>
 7 <body>
 8 <!--
 9 repeat()
10 作用:对字符串进行重复
11 参数:count(重复次数)
12 返回值:重复操作之后的字符串
13 
14 
15 -->
16 <script>
17     // console.log('abc'.repeat(0));
18     // console.log('abc'.repeat(1));
19     // console.log('abc'.repeat(2));//'abcabc'
20 
21 
22     //手写repeat方法
23     String.prototype.repeat_my = function(count){
24         let ans_str='';
25         console.log(this);
26         for(let i=0;i<count;i++){
27             ans_str+=this;
28         }
29         return ans_str;
30     };
31     console.log('abc'.repeat_my(4));
32     // 'abc'.repeat_my(2);
33 
34 </script>
35 </body>
36 </html>
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/12690579.html