What is the more elegant way to remove all characters after specific character in the String
object in Swift?
Suppose that I have the following string:
I think this is better approach:
Update with Swift 4: (substring is now deprecated)
let smth = "element=value"
if let index = (smth.range(of: "=")?.upperBound)
{
//prints "value"
let afterEqualsTo = String(smth.suffix(from: index))
//prints "element="
let beforeEqualsToContainingSymbol = String(smth.prefix(upTo: index))
}
if let index = (smth.range(of: "=")?.lowerBound)
{
//prints "=value"
let afterEqualsToContainingSymbol = String(smth.suffix(from: index))
//prints "element"
let beforeEqualsTo = String(smth.prefix(upTo: index))
}