./src/share/classes/java/lang/String.java

Print this page
rev 5707 : [mq]: StringRepeat

@@ -2360,10 +2360,26 @@
     public String[] split(String regex) {
         return split(regex, 0);
     }
 
     /**
+     * Set the value of this {@code String} to {@code n} concatenated copies of the
+     * current value.  If {@code n == 0}, then set the value to the empty string.
+     * 
+     * @param n the number of times to add the current value
+     * @return a reference to this String
+     * @throws IllegalArgumentException if n < 0
+     * @since 1.8
+     */
+    public String repeat( int n ) {
+        if (n < 0) {
+            throw new IllegalArgumentException( "n < 0");
+        }
+        return new StringBuilder().append(n, this).toString();
+    }
+
+    /**
      * Converts all of the characters in this {@code String} to lower
      * case using the rules of the given {@code Locale}.  Case mapping is based
      * on the Unicode Standard version specified by the {@link java.lang.Character Character}
      * class. Since case mappings are not always 1:1 char mappings, the resulting
      * {@code String} may be a different length than the original {@code String}.