itsdevthen
itsdevthen

Reputation: 79

Double quotes(escape character) inside String(format: ) in swift

\ is not working inside String(format:) for escaping character.

How to put Double quotes(escape character) inside String(format: ) in swift?

Example

let timeString = String(format: "https://stackoverflow.com/character=\"inside\"")

I need to pass url parameter without double quotes inside String(format:) so I need to escape the double quotes inside

I get stackoverflow.com/character="inside" I need stackoverflow.com/character=inside

I input with quotes as String(format: 'https://stackoverflow.com/character="inside"') but while passing to url i should pass it without quotes as
String(format: 'https://stackoverflow.com/character=inside
because in url it shows as
https://stackoverflow.com/character="inside"

Upvotes: 1

Views: 1075

Answers (3)

Vaisakh KP
Vaisakh KP

Reputation: 497

Simply use string interpolation. You can flexibly pass integer, string, double etc

let timeString = String(format: "https://stackoverflow.com/character=\(inside)")

Refer to: String Interpolation in swift

Upvotes: 1

Tomte
Tomte

Reputation: 1339

Just do:

let timeString = String(format: "https://stackoverflow.com/character=\"inside\"").replacingOccurrences(of: "\"", with: "")

This will output as:

https://stackoverflow.com/character=inside

Replace the occurrences of " with nothing.

Happy coding!

Upvotes: 1

Gareth Miller
Gareth Miller

Reputation: 402

You can do:

let timeString = String(format: "https://stackoverflow.com/character=inside")

Or am I missing something?

Upvotes: 0

Related Questions