Skrai2004
Skrai2004

Reputation: 39

Join list into one string

How to remove the first word of a string?

0.91% ABC DEF
0.922% ABC DEF GHI

OUTPUT: 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

Answers (2)

Luca Angeletti
Luca Angeletti

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

Scriptable
Scriptable

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

Related Questions