Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Create String given length same character [duplicate]

Tags:

string

swift

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);

like image 407
Marcus Leon Avatar asked Nov 21 '16 01:11

Marcus Leon


1 Answers

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)
like image 62
vacawama Avatar answered Nov 15 '22 08:11

vacawama