Roshni
Roshni

Reputation: 153

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()

Upvotes: 4

Views: 3825

Answers (2)

Kuldeep Choudhary
Kuldeep Choudhary

Reputation: 71

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: #"\""#)

Upvotes: 5

Akshay Digrase
Akshay Digrase

Reputation: 468

Instead of putting the name (i.e., "XYZ" if you getting xyz from textfield ) why not to place (textField.text!) it will not put extra " "

Upvotes: 0

Related Questions