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

 
(사용자 2명의 중간 판 3개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;Java str_repeat()
;Java str_repeat()
<source lang='Java'>
 
<syntaxhighlight lang='Java' run>
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) ); // hellohellohello
         System.out.println( repeat(str,3) );
        // hellohellohello
     }
     }
}
}
</source>
</syntaxhighlight>
<source lang='Java'>
<syntaxhighlight lang='Java' run>
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);
21번째 줄: 20번째 줄:
     }
     }
     public static void main(String args[]) {
     public static void main(String args[]) {
        String str = "hello";
         System.out.println( str_repeat("hello",3) ); // hellohellohello
         System.out.println( repeat(str,3) );
        // hellohellohello
     }
     }
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

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