Home > front end >  Swift: How to remove all text in a string before comma
Swift: How to remove all text in a string before comma

Time:08-01

I have a String : "Doe, John". How would I remove all of the text before the word "John" so that it would be "John".

CodePudding user response:

let str = "John, Doe"
let array = str.split(separator: ",")
print("\(array.last?.trimmingCharacters(in: .whitespaces) ?? "")")

prints

Doe
Program ended with exit code: 0

I would also recommend using the documentation Larme has provided in the comments

  • Related