Swift:给定长度的相同字符创建字符串

标签 swift string

<分区>

如何在不使用循环的情况下创建由相同字符组成的给定长度的字符串?

即:创建一个长度为 10 个字符的字符串,其中每个字符都是一个星号:**********

类似于此approach in Java : new String(new char[n]).replace("\0", s);

最佳答案

有一个 String 初始化程序:

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)

关于Swift:给定长度的相同字符创建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711645/

相关文章:

创建一个程序,要求用户输入名称并使用 C 创建 10 个名称序列化的文件

ios - Swift 3 - 每次添加位置坐标时 UIView 都会加载

ios - 无法使用多个 Match 功能

javascript - 如何在 PHP 中获取特定的字符串单词?

ruby - 在 Ruby 中如何查找一个字符串是否以另一个字符串开头?

python - 使用 bs4 和 requests 仅获取原始链接

ruby-on-rails - string vs text using Rails 3.2.* and Postgres - 我应该总是只使用文本吗

Swift 游戏音量/扬声器图标

ios - Xcode 访问用户定义的运行时属性中的属性

ios - 如何立即隐藏单个 View Controller 上的导航栏?