"함수 str repeat()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
(같은 사용자의 중간 판 8개는 보이지 않습니다)
6번째 줄: 6번째 줄:
[[분류: Java]]
[[분류: Java]]
{{참고|Java str_repeat()}}
{{참고|Java str_repeat()}}
<syntaxhighlight lang='Java'>
<syntaxhighlight lang='Java' run>
public class MyClass {
public class MyClass {
     public static String str_repeat(String str, int times) {
     public static String str_repeat(String str, int times) {
12번째 줄: 12번째 줄:
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
         System.out.println( str_repeat("hello",3) );
         System.out.println( str_repeat("hello",3) ); // hellohellohello
        // hellohellohello
     }
     }
}
}
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='Java'>
<syntaxhighlight lang='Java' run>
public class MyClass {
public class MyClass {
     public static String str_repeat(String str, int times) {
     public static String str_repeat(String str, int times) {
25번째 줄: 24번째 줄:
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
         System.out.println( str_repeat("hello",3) );
         System.out.println( str_repeat("hello",3) ); // hellohellohello
        // hellohellohello
     }
     }
}
}
33번째 줄: 31번째 줄:
==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<syntaxhighlight lang='JavaScript'>
<syntaxhighlight lang='JavaScript' run>
var a = "hello";
var b = new Array(3+1).join(a);
console.log( b ); // hellohellohello
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
String.prototype.repeat=function(n){return new Array(n+1).join(this)}
String.prototype.repeat=function(n){return new Array(n+1).join(this)}
console.log( "hello".repeat(3) );
console.log( "hello".repeat(3) ); // hellohellohello
// hellohellohello
</syntaxhighlight>
</syntaxhighlight>


==Perl==
==Perl==
[[분류: Perl]]
[[분류: Perl]]
<syntaxhighlight lang='Perl'>
<syntaxhighlight lang='Perl' run>
print "hello" x 3
print "hello" x 3 # hellohellohello
# hellohellohello
</syntaxhighlight>
</syntaxhighlight>


49번째 줄: 50번째 줄:
[[category: PHP]]
[[category: PHP]]
{{참고|PHP str_repeat()}}
{{참고|PHP str_repeat()}}
<syntaxhighlight lang='PHP'>
<syntaxhighlight lang='PHP' run>
echo str_repeat("hello", 3);
echo str_repeat("hello", 3); // hellohellohello
// hellohellohello
</syntaxhighlight>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<syntaxhighlight lang='Python'>
<syntaxhighlight lang='Python' run>
print "hello" * 3
print( "hello" * 3 ) # hellohellohello
# hellohellohello
</syntaxhighlight>
</syntaxhighlight>



2021년 4월 14일 (수) 18:50 판


함수 str_repeat()

1 Java

public class MyClass {
    public static String str_repeat(String str, int times) {
        return new String(new char[times]).replace("\0", str);
    }
    public static void main(String args[]) {
        System.out.println( str_repeat("hello",3) ); // hellohellohello
    }
}
public class MyClass {
    public static String str_repeat(String str, int times) {
        StringBuilder sb = new StringBuilder(str.length() * times);
        for (int i = 0; i < times; i++) sb.append(str);
        return sb.toString();
    }
    public static void main(String args[]) {
        System.out.println( str_repeat("hello",3) ); // hellohellohello
    }
}

2 JavaScript

var a = "hello";
var b = new Array(3+1).join(a);
console.log( b ); // hellohellohello
String.prototype.repeat=function(n){return new Array(n+1).join(this)}
console.log( "hello".repeat(3) ); // hellohellohello

3 Perl

print "hello" x 3 # hellohellohello

4 PHP

echo str_repeat("hello", 3); // hellohellohello

5 Python

print( "hello" * 3 ) # hellohellohello

6 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}