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

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
2번째 줄: 2번째 줄:
;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) {
8번째 줄: 8번째 줄:
     }
     }
     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) {
21번째 줄: 20번째 줄:
     }
     }
     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
     }
     }
}
}

2021년 4월 14일 (수) 18:49 기준 최신판

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