3

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: "&lt;")
strToReturn = strToReturn.replacingOccurrences(of: ">", with: "&gt;")
strToReturn = strToReturn.replacingOccurrences(of: "‘", with: "&apos;")
strToReturn = strToReturn.replacingOccurrences(of: "“", with: "&quot;") 

print("Replaced string : \(strToReturn)") 

Result is &quot;Hello” &apos;world’

If anyone can help, thanks!

Dhaval H. Nena
  • 3,992
  • 1
  • 37
  • 50

5 Answers5

10

You need to specify replacement strings for and because ’ != ‘ and ” != “

var strToReturn = "“Hello” ‘world’"
strToReturn = strToReturn.replacingOccurrences(of: "&", with: "&amp;")
strToReturn = strToReturn.replacingOccurrences(of: "<", with: "&lt;")
strToReturn = strToReturn.replacingOccurrences(of: ">", with: "&gt;")
strToReturn = strToReturn.replacingOccurrences(of: "‘", with: "&apos;")
strToReturn = strToReturn.replacingOccurrences(of: "“", with: "&quot;")
strToReturn = strToReturn.replacingOccurrences(of: "’", with: "&apos;")
strToReturn = strToReturn.replacingOccurrences(of: "”", with: "&quot;") 
Kamran
  • 14,987
  • 4
  • 33
  • 51
2

The reason for this is because is different than and is different than . So you need to add these lines too.

strToReturn = strToReturn.replacingOccurrences(of: "’", with: "&apos;")
strToReturn = strToReturn.replacingOccurrences(of: "”", with: "&quot;") 

This will give you the expected result

hardik parmar
  • 853
  • 8
  • 15
2

If you print the ascii values of the string you will see the quotes are not the same unicode character. So make sure you use the same unicode character or handle both case

strToReturn.characters.map{print($0, "\(String($0.unicodeScalars.first!.value, radix: 16, uppercase: true))")}

“ 201C
H 48
e 65
l 6C
l 6C
o 6F
” 201D
  20
‘ 2018
w 77
o 6F
r 72
l 6C
d 64
’ 2019
ugur
  • 3,604
  • 3
  • 26
  • 57
1

Your code is working perfectly fine with me . I just changed the strings as I mentioned in the comments :

For anyone wondering how the Single and Double quotes inside the strings are generated --- Hold down alt/option and press Square / Curly bracket keys

Just change the letters using the key combination and it will work

enter image description here

Shubham Bakshi
  • 552
  • 3
  • 10
0

the below extension is very useful

 var toReplaceSmartQuotes: String {
        return self.replacingOccurrences(of: "‘", with: "'").replacingOccurrences(of: "’", with: "'")
            .replaceCharacters(characters: "”", toSeparator: #"""#)
    }
    
    func replaceCharacters(characters: String, toSeparator: String) -> String {
        let characterSet = CharacterSet(charactersIn: characters)
        let components = components(separatedBy: characterSet)
        let result = components.joined(separator: toSeparator)
        return result
    }
Metin Atalay
  • 1,375
  • 18
  • 28