Any way to replace characters on Swift String?

前端 未结 21 1858
忘了有多久
忘了有多久 2020-11-22 04:59

I am looking for a way to replace characters in a Swift String.

Example: \"This is my string\"

I would like to replace \" \" with \"+\" to get \

21条回答
  •  孤独总比滥情好
    2020-11-22 05:28

    Swift 3, Swift 4, Swift 5 Solution

    let exampleString = "Example string"
    
    //Solution suggested above in Swift 3.0
    let stringToArray = exampleString.components(separatedBy: " ")
    let stringFromArray = stringToArray.joined(separator: "+")
    
    //Swiftiest solution
    let swiftyString = exampleString.replacingOccurrences(of: " ", with: "+")
    

提交回复
热议问题