DEV Community

Cover image for [Swift CODE] URLify
✨ thetealpickle πŸ“±
✨ thetealpickle πŸ“±

Posted on

[Swift CODE] URLify

THE TEAL PICKLE CODING CHALLENGE!! Replace all spaces on a string with β€˜%20'. I solved this problem with my lover, Swift. TRY IT πŸ‘€

QUESTION:

MY SOLUTION:

Top comments (2)

Collapse
Β 
tobiassn profile image
Tobias SN β€’ β€’ Edited

Uhhh...

string = string.replace(β€œ β€œ, β€œ%20”)

Or if you wanna go all in:

from urllib.parse import quote
string = quote(string)

(Snippets are in Python because I don’t know Swift)

Collapse
Β 
thetealpickle profile image
✨ thetealpickle πŸ“± β€’ β€’ Edited

Yeah in Swift 5 the syntax is pretty similar
string.replaceOccurrences(of: β€œ β€œ, with: β€œ%20”)

In everyday practice, I def use that method.

Typically in interviews, interviewers want to see whether the interviewee understands/can derive the algorithm which makes that method work (hence, all the code πŸ˜¬πŸ™ƒ)