String.repeat
func String.repeat(s: string, n: int): stringString.repeat returns s repeated n times. It panics if n < 0, or if n or the result's length in bytes is above 2^31 - 1.
Details
- An
nof0, or an emptys, gives"". - Panics if
nis negative. ncounts copies, not code points. To fill up to a width, useString.padStartorString.padEnd.
Basic examples
Repeat a string three times:
String.repeat("ab", 3) // "ababab"Call it through . on any string:
"-".repeat(10) // "----------"Scope
Zero copies give the empty string, and one copy gives s itself:
String.repeat("ab", 0) // ""
String.repeat("ab", 1) // "ab"An empty s gives the empty string for any count, even the largest:
String.repeat("", 5) // ""
String.repeat("", 2147483647) // ""s may be text in any script, an emoji, or an escape such as \n:
String.repeat("日本", 2) // "日本日本"
String.repeat("🐜", 3) // "🐜🐜🐜"
String.repeat("ab\n", 2) // "ab\nab\n"The count may be any int expression:
let word = "ant"
String.repeat("^", word.length()) // "^^^"On a string that may be null, call it through ?.; the result is null when the string is:
let name: string | null = null
name?.repeat(2) // null
let tag: string | null = "ab"
tag?.repeat(2) // "abab"In a pipeline, the string is the first argument:
"ha" | String.repeat(3) // "hahaha"Named arguments
Name the arguments s and n, in either order:
String.repeat(s: "x", n: 4) // "xxxx"
String.repeat(n: 2, s: "o") // "oo"Applications
Underline a title with a rule as long as the title:
func heading(title: string): string {
"${title}\n${"=".repeat(title.length())}"
}
println(heading("Ant docs"))Ant docs
========Indent a line by its depth in a tree:
type Item = { name: string, depth: int }
let items = [Item{ name: "src", depth: 0 }, Item{ name: "lib", depth: 1 }, Item{ name: "a.ant", depth: 2 }]
for item in items {
println("${" ".repeat(item.depth)}${item.name}")
}src
lib
a.antDraw a progress bar from two repeated pieces:
func bar(done: int, total: int): string {
"[${"#".repeat(done)}${".".repeat(total - done)}]"
}
bar(3, 10) // "[###.......]"
bar(10, 10) // "[##########]"Print a small bar chart:
type Count = { name: string, count: int }
let counts = [Count{ name: "a", count: 3 }, Count{ name: "b", count: 5 }, Count{ name: "c", count: 1 }]
for c in counts {
println("${c.name} ${"*".repeat(c.count)}")
}a ***
b *****
c *Properties and relations
The length of the result is n times the length of s:
for s in ["", "a", "日本", "🐜!"] {
for n in [0, 1, 4] {
assert(s.repeat(n).length() == n * s.length())
}
}Repeating a + b times is repeating a times and then b times:
let s = "xy"
let a = 2
let b = 3
assert(s.repeat(a + b) == s.repeat(a) + s.repeat(b))It is String.concat of an array of n copies:
let s = "ab"
assert(s.repeat(4) == String.concat(Array.filled(4, s)))Repeating a repeat multiplies the counts:
assert("ab".repeat(2).repeat(3) == "ab".repeat(6))For a single code point, it is String.padStart of the empty string:
assert(String.repeat("*", 5) == String.padStart("", 5, "*"))Possible issues
A negative count panics; it does not give the empty string:
String.repeat("ab", -1) // panic: String.repeat needs a count of at least 0, got -1Clamp a count that may go below zero with Math.max:
let width = 4
let title = "library"
String.repeat(".", Math.max(width - title.length(), 0)) // ""A count above 2^31 − 1 panics:
String.repeat("a", 2147483648) // panic: String.repeat needs a count of at most 2147483647, got 2147483648A count that is in range still panics when the result would be longer than 2^31 − 1 bytes:
String.repeat("ab", 1073741824) // panic: String.repeat would give a string longer than 2147483647 bytesn counts copies, not code points, so a string of several code points overshoots a width. Use String.padEnd to fill an exact width:
String.repeat("ab", 5) // "ababababab"
String.padEnd("", 5, "ab") // "ababa"The count is an int. A float64, or an int that may be null, is a compile error:
let w = 2.5
let s = String.repeat("a", w) // error: cannot use float64 where int is expectedlet i = String.indexOf("a.b", ".")
let s = String.repeat("-", i) // error: cannot use int | null where int is expectedGive a default with ?? to turn the int | null into an int:
String.repeat("-", "a.b".indexOf(".") ?? 0) // "-"Repeating a combining mark does not give separate characters: every copy joins the grapheme before it:
let s = "e" + "\u{301}".repeat(3)
s.length() // 4
len(String.graphemes(s)) // 1See also
String.padStart · String.padEnd · String.concat · String.join · Array.filled · String.length
