Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to replace double quotes with a backslash in swift

I have a submit form where there are multiple textfields. Whenever user enters text like "Hi, my name is "xyz"", the service does not accept this JSON due to double quotes in my string. Please suggest ways to escape this character. I have tried using encode and decode JSON, replaceOccurrencesOf methods, but none work.

replaceOccurrencesOf()

like image 220
Roshni Avatar asked Oct 18 '25 12:10

Roshni


1 Answers

The below code snippet with replace "(double quote) in a string by \". This will help to replace "(double quote) by any string or character in a given string.

Swift 5 or above
let replacedString = stringToBeModified.replacingOccurrences(of: "\"", with: #"\""#)

like image 101
Kuldeep Choudhary Avatar answered Oct 21 '25 00:10

Kuldeep Choudhary