我正在尝试使用 swift 5 将数组中的多个元素与另一个字符串连接起来:例如:
MyArray = ["Bat","Foot","Basket"]
MyString = "Ball"
output = ["BatBall","FootBall","BasketBall"]
Run Code Online (Sandbox Code Playgroud)
或者
output = ["BallBat","BallFoot","BallBasket"]
Run Code Online (Sandbox Code Playgroud)
我该怎么做?请帮忙。
小智 6
您可以通过地图功能实现这一点:
var myArray = ["Bat","Foot","Basket"]
var output = myArray.map { $0 + "ball" }
print(output) // ["Batball", "Football", "Basketball"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |