Swift에서 replace는 어떻게 하나 하면서 찾아본...

역시나 있다...


그것도 아주 이쁘게..


You can use this:

let s = "This is my string"
let modified = s.replace(" ", withString:"+")    

If you add this extension method anywhere in your code:

extension String
{
    func replace(target: String, withString: String) -> String
    {
       return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
}

Swift 3:

extension String
{
    func replace(target: String, withString: String) -> String
    {
        return self.replacingOccurrences(of: target, with: withString, options: NSString.CompareOptions.literal, range: nil)
    }
}


실제 링크는 여기

http://stackoverflow.com/questions/24200888/any-way-to-replace-characters-on-swift-string


Swift2


Swift3



+ Recent posts