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

2번째 줄: 2번째 줄:
{{DISPLAYTITLE:함수 str_repeat()}}
{{DISPLAYTITLE:함수 str_repeat()}}
;함수 str_repeat()
;함수 str_repeat()
==Java==
[[분류: Java]]
{{참고|Java str_repeat()}}
<source lang='Java'>
public class MyClass {
    public static String repeat(String str, int times) {
        return new String(new char[times]).replace("\0", str);
    }
    public static void main(String args[]) {
        String str = "hello";
        System.out.println( repeat(str,3) );
        // hellohellohello
    }
}
</source>
<source lang='Java'>
public class MyClass {
    public static String 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[]) {
        String str = "hello";
        System.out.println( repeat(str,3) );
        // hellohellohello
    }
}
</source>


==JavaScript==
==JavaScript==

2018년 8월 12일 (일) 21:52 판


함수 str_repeat()

1 Java

public class MyClass {
    public static String repeat(String str, int times) {
        return new String(new char[times]).replace("\0", str);
    }
    public static void main(String args[]) {
        String str = "hello";
        System.out.println( repeat(str,3) );
        // hellohellohello
    }
}
public class MyClass {
    public static String 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[]) {
        String str = "hello";
        System.out.println( repeat(str,3) );
        // hellohellohello
    }
}

2 JavaScript

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 }}