将列表加入一个字符串

时间:2017-08-30 11:07:31

标签: arrays swift string join

如何删除字符串的第一个单词?

  

0.91%ABC DEF
  0.922%ABC DEF GHI

     

输出:ABC DEF / ABC DEF GHI

我试过

let test = str.split(separator: " ")[1...]
print(test)
print(test.joined(separator: " "))

这给了我:

["ABC", "DEF"]
JoinedSequence<ArraySlice<Substring>>(_base: ArraySlice(["ABC", "DEF"]), _separator: ContiguousArray([" "]))

如何将JoinedSequence打印为字符串?

2 个答案:

答案 0 :(得分:2)

试试这个:

let str = "0.91% ABC DEF"
var parts = str.components(separatedBy: " ").dropFirst()

print(parts.joined(separator: " "))

打印哪些:

  

“ABC DEF \ n”

答案 1 :(得分:0)

给定一个字符串

if let index = text.range(of: " ")?.upperBound {
    let result = text.substring(from: index)
    print(result)
}

你可以在第一个空格后搜索'index'

flag