arrays - 如何从字符串创建一个单独的字符串数组
问题描述
这在标题中很难解释,但我有一个这样的字符串:
"One two three four"
如何拆分它,使其产生如下数组:
["four", "three four", "two three four", "one two three four"]
解决方案
你可以试试
let str = "One two three four".components(separatedBy: " ")
let res = str.indices.map { str[0...$0].joined(separator: " ") }
print(res)
let str = Array("One two three four".components(separatedBy: " ").reversed())
let res = str.indices.map { str[0...$0].reversed().joined(separator: " ") }
print(res)
推荐阅读
- c# - 数学运算返回 0
- javascript - 试图在javascript中将div块宽度更改为屏幕宽度以使其响应
- android - 我被困在这个登录页面
- python - 从管道中的项目容器打印变量时出错,python scrapy
- apache-spark - 如何访问数据源选项(例如 kafka)?
- oracle - 如何在excel中运行oracle 12c报告
- powershell - 如何在 PS 中使用 Curl
- xpages - OpenNTF Domino API 崩溃 Domino Designer V10
- python - 在培训期间,我收到以下错误。“ ValueError:没有为任何变量提供渐变”
- javascript - 从对象生成的 UL 中添加和删除项目