Recent posts

Recent comments

Archive

Calender

«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Use the Google Translate, if you want to read this page in your language.
But its translation is probably inaccurate.
Go to the Google Translate.


[ Javascript ] repeat() of the string




Polyfill

 
<script type="text/javascript">
/* 
 * source : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/repeat 
*/ 

if ( ! String.prototype.repeat ){ 
     String.prototype.repeat = function repeat ( count ){ 

          var str = "" + this; 
          var count = Math.floor( count ); 
          if ( str.length == 0 || count == 0 || isNaN( count ) ){  return "";  }; 

          var message = ""; 
          if ( count < 0 ){ message = "repeat count can't be a negative."; }; 
          if ( count == Infinity ){ message = "repeat count must be less than infinity."; }; 
          if ( str.length * count >= 1 << 28 ){ message = "Invalid length of string."; }; 

          if ( message.length > 0 ){ 
               try {  throw new Error( message );  } 
               catch( error ) { 
                    error.name = "RangeError"; 
                    throw error; 
               } 
          }

          var repeat = ""; 
          for ( ; ; ){ 
               if ( (count & 1) == 1 ){  repeat += str;  } 
               count >>>= 1; 
               if ( count == 0 ){  break;  } 
               str += str; 
          } 

          return repeat; 
     }; 
} 
</script>
 


Example 1

repeat() 함수를 사용하면,
원본 문자열을 복사한 후, 원하는 횟수만큼 반복해서 이어붙일 수 있다.
Using repeat() function,
you can copy the original string and splice it as many times as you want.


 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">

function testing1 (){ 
    var string = "Hi everyone. "; 
    var paste = string.repeat( 2 ); 
    demo1.innerHTML = paste; 
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">

function testing2 (){ 
    var string = "Blah "; 
    var paste = string.repeat( 5 ); 
    demo2.innerHTML = paste; 
} 
</script>



Example 2

원본 문자열이 아무것도 없는 빈 문자열( "" )이거나, 반복 횟수가 "0"일 경우,
아무것도 복사하지 말라는 뜻이 돼버리므로
실행 결과는 빈 문자열이다.
If the string is empty string or the repeat count is zero(0),
it returns empty string.
Because it means that there is nothing to copy or that you don't want to copy.


 
<button onclick="testing1()"> Click me </button>

<p id="copy1"> </p>
<p id="type1"> </p>
<p id="length1"> </p>

<script type="text/javascript">

function testing1 (){ 
    var string = ""; 
    var result = string.repeat( 5 ); 

    copy1.innerHTML = result; 
    type1.innerHTML = typeof result; 
    length1.innerHTML = result.length; 
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="copy2"> </p>
<p id="type2"> </p>
<p id="length2"> </p>

<script type="text/javascript">

function testing2 (){ 
    var string = "Hi everyone. "; 
    var result = string.repeat( 0 ); 

    copy2.innerHTML = result; 
    type2.innerHTML = typeof result; 
    length2.innerHTML = result.length; 
} 
</script>



Example 3

반복 횟수인 count가 음수이거나 Infinity이라면, 에러가 난다.
If the repeat count is a negative number or Infinity, an error occurs.


 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">

function testing1 (){ 
    var string = "Hi everyone. "; 
    try {  var result = string.repeat( -5 );  } 
    catch( error ) {  demo1.innerHTML = error.message;   } 
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">

function testing2 (){ 
    var string = "Hi everyone. "; 
    try {  var result = string.repeat( Infinity );  } 
    catch( error ) {  demo2.innerHTML = error.message;   } 
} 
</script>



Example 4

복사해서 이어붙인 문장의 전체 길이가 268,435,456( 0x10,000,000 ) 이상일 경우에도 에러가 난다.
If the length of copied string is 268,435,456( 0x10,000,000 ) or more, an error occurs.


 
<button onclick="testing1()"> Click me </button>

<p id="demo1"> </p>

<script type="text/javascript">

function testing1 (){ 
    var count = Math.pow( 2, 28 ) - 1; 

    var string = "H"; 
    try { 
        var result = string.repeat( count ); 
        demo1.innerHTML = result.length; 
    } 
    catch( error ) {  demo1.innerHTML = error.message;   } 
} 
</script>
 
<button onclick="testing2()"> Click me </button>

<p id="demo2"> </p>

<script type="text/javascript">

function testing2 (){ 
    var count = Math.pow( 2, 28 ); 

    var string = "H"; 
    try { 
        var result = string.repeat( count ); 
        demo2.innerHTML = result.length; 
    } 
    catch( error ) {  demo2.innerHTML = error.message;   } 
} 
</script>


 
<button onclick="testing()"> Click me </button>

<p id="bit"> </p>
<p id="dec"> </p>
<p id="hex"> </p>

<script type="text/javascript">

function testing (){ 

    var bitwise = 1 << 28; 

    var decimal = Math.pow( 2, 28 ); 

    var hexadecimal = 0x10000000; 

    bit.innerHTML = bitwise; 
    dec.innerHTML = decimal; 
    hex.innerHTML = hexadecimal; 
} 
</script>
 


https://tonks.tistory.com/168#_javascript_string_repeat

이 내용이 도움이 되셨다면, 아래의 하트 버튼을 눌러주세요. *^^*
If this article is helpful to you, please click the heart button below. *^^*