"Java str repeat()"의 두 판 사이의 차이

1번째 줄: 1번째 줄:
==개요==
==개요==
;Java str_repeat()
;Java str_repeat()
<source lang='Java'>
<source lang='Java'>
public class MyClass {
public class MyClass {
     public static String repeat(String str, int times) {
     public static String str_repeat(String str, int times) {
         return new String(new char[times]).replace("\0", str);
         return new String(new char[times]).replace("\0", str);
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
        String str = "hello";
         System.out.println( str_repeat("hello",3) );
         System.out.println( repeat(str,3) );
         // hellohellohello
         // hellohellohello
     }
     }
15번째 줄: 15번째 줄:
<source lang='Java'>
<source lang='Java'>
public class MyClass {
public class MyClass {
     public static String repeat(String str, int times) {
     public static String str_repeat(String str, int times) {
         StringBuilder sb = new StringBuilder(str.length() * times);
         StringBuilder sb = new StringBuilder(str.length() * times);
         for (int i=0; i<times; i++) sb.append(str);
         for (int i = 0; i < times; i++) sb.append(str);
         return sb.toString();
         return sb.toString();
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
        String str = "hello";
         System.out.println( str_repeat("hello",3) );
         System.out.println( repeat(str,3) );
         // hellohellohello
         // hellohellohello
     }
     }

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

1 개요

Java str_repeat()
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 같이 보기

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