Reputation: 39
How to remove the first word of a string?
0.91% ABC DEF
0.922% ABC DEF GHIOUTPUT: ABC DEF / ABC DEF GHI
I tried
let test = str.split(separator: " ")[1...]
print(test)
print(test.joined(separator: " "))
Which gives me:
["ABC", "DEF"]
JoinedSequence<ArraySlice<Substring>>(_base: ArraySlice(["ABC", "DEF"]), _separator: ContiguousArray([" "]))
How can I print the JoinedSequence as a string?
Upvotes: 0
Views: 64
Reputation: 59496
Given a string
let text = "0.91% ABC DEF"
you can search for the ´index´ after the first space
if let index = text.range(of: " ")?.upperBound {
let result = text.substring(from: index)
print(result)
}
Upvotes: 0
Reputation: 19750
Try this:
let str = "0.91% ABC DEF"
var parts = str.components(separatedBy: " ").dropFirst()
print(parts.joined(separator: " "))
Which prints:
"ABC DEF\n"
Upvotes: 2