./src/share/classes/java/lang/String.java
Print this page
rev 5707 : 6984084 (str) n times repetition of character constructor for java.lang.String
Reviewed-by: duke
Contributed-by: Jim Gish <jim.gish@oracle.com>
@@ -2360,10 +2360,34 @@
public String[] split(String regex) {
return split(regex, 0);
}
/**
+ * Returns a new {@code String} whose value is {@code n} concatenated copies of the
+ * current value. If {@code n == 0}, then an empty string is returned.
+ * If {@code n == 1}, then the current {@code String} is returned.
+ *
+ * @param n the number of times the current value is concatenated to itself
+ * @return a reference to this String or a new String depending on the value of n
+ * @throws IllegalArgumentException if n < 0
+ * @since 1.8
+ */
+ public String repeat( int n ) {
+ if (n < 0) {
+ throw new IllegalArgumentException( "n < 0");
+ }
+ if (n == 0) {
+ return "";
+ }
+ if (n == 1) {
+ return this;
+ }
+
+ 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}.