Any way to replace characters on Swift String?

前端 未结 21 2159
忘了有多久
忘了有多久 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:26

    Swift 4:

    let abc = "Hello world"
    
    let result = abc.replacingOccurrences(of: " ", with: "_", 
        options: NSString.CompareOptions.literal, range:nil)
    
    print(result :\(result))
    

    Output:

    result : Hello_world
    

提交回复
热议问题