How can you create a String of a given length consisting of the same character - without using a loop?
Ie: create a String 10 characters in length where each character is an asterisk: **********
Similar to this approach in Java: new String(new char[n]).replace("\0", s);
There's a String
initializer for that:
init(repeating repeatedValue: String, count: Int)
Description
Creates a new string representing the given string repeated the specified number of times.
let string = String(repeating: "*", count: 10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With