Here is my question, I have a string array that contains a bunch of countries:
let myCountryStart = ["Africa/ABC", "America/BBC", "Asia/CBC", "Pacific/CBA", "Europe/CBB", "Indian/CAB"]
Is there have any solution to remove the specific words like 'Africa', 'America', 'Asia'...etc,. Let the output result looks like the below followings:
let myCountryEnd = ["ABC", "BBC", "CBC", "CBA", "CBB", "CAB"]
Here are my code for now...
let 1stReplace = myCountryStart.replacingOccurrences(of: "/", with: "", options: .literal)
let 2ndReplace = 1stReplace.replacingOccurrences(of: "Africa", with: "", options: .literal)
let 3rdReplace = 2ndReplace.replacingOccurrences(of: "Asia", with: "", options: .literal)
I know this is a stupid solution. Hence, I prefer to use NSRegular Expression. But I encountered a problem about String & String Array issue.
let target = myCountryStart
let regex = "/"
let RE = try? NSRegularExpression(pattern: "regex", options: .caseInsensitive)
let modified = RE?.stringByReplacingMatches(in: target, options: .reportProgress, range: nil, withTemplate: "") {
return modified
}
let myCountryEnd = modified
Therefore, I got a warning about I cannot use this method on String array. What should I do to fix it?
Any suggestions or help will be greatly appreciated. Thanks from a Swift rookie.
myCountryStartseems to only have one element. Is that true?outputStrin your old code? Why don't you use that, instead ofmyCountryStartin your new code?