Repeat a piece of text
Repeat the same text a given number of times. The new string contains each copy in order.
String.init(repeating:count:)
Simple example
Repeat x three times.
Start'x' · 3
Resultxxx
import Foundation
print(String(repeating: "x", count: 3))
xxx
Advertisements
Everyday examples
Print four star marks on a prize ticket.
Start'*' · 4
Result****
import Foundation
print(String(repeating: "*", count: 4))
****
Draw a six-character divider for a receipt.
Start'-' · 6
Result------
import Foundation
print(String(repeating: "-", count: 6))
------
Make a three-part red-blue pattern for a ribbon label.
Start'RB' · 3
ResultRBRBRB
import Foundation
print(String(repeating: "RB", count: 3))
RBRBRB