Trying to escape few special characters of string for sending it via xml api.
Tried below code but not working for all the occurrences of Single Quote (') and Double Quote (")
var strToReturn = "“Hello” ‘world’"
strToReturn = strToReturn.replacingOccurrences(of: "&", with: "&")
strToReturn = strToReturn.replacingOccurrences(of: "<", with: "<")
strToReturn = strToReturn.replacingOccurrences(of: ">", with: ">")
strToReturn = strToReturn.replacingOccurrences(of: "‘", with: "'")
strToReturn = strToReturn.replacingOccurrences(of: "“", with: """)
print("Replaced string : \(strToReturn)")
Result is "Hello” 'world’
If anyone can help, thanks!
You need to specify replacement strings for ’
and ”
because ’ != ‘
and ” != “
var strToReturn = "“Hello” ‘world’"
strToReturn = strToReturn.replacingOccurrences(of: "&", with: "&")
strToReturn = strToReturn.replacingOccurrences(of: "<", with: "<")
strToReturn = strToReturn.replacingOccurrences(of: ">", with: ">")
strToReturn = strToReturn.replacingOccurrences(of: "‘", with: "'")
strToReturn = strToReturn.replacingOccurrences(of: "“", with: """)
strToReturn = strToReturn.replacingOccurrences(of: "’", with: "'")
strToReturn = strToReturn.replacingOccurrences(of: "”", with: """)