简体   繁体   中英

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"

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!

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

You can do:

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

Or am I missing something?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM