Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active July 10, 2021 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/5ae4baaa2b01419227f7d943f01f400f to your computer and use it in GitHub Desktop.
Save krzyzanowskim/5ae4baaa2b01419227f7d943f01f400f to your computer and use it in GitHub Desktop.
In Swift \r\n is a single Character, just like \n
let str = "dupa\r\nblada"
for i in str.indices {
switch str[i] {
case "\n":
print("\\n found") // \n not found
case "\r":
print("\\r found") // \r not found
case "\r\n":
print("\\r\\n found") // found
default:
break
}
}
// if that'd be a simple tech interview question where you need to find \n, you guts are wrong and you failed
// To make life easier, treat string as collection of utf8 values
for i in Array(str.utf8).indices {
switch Array(str.utf8)[i] {
case "\n".utf8.first!:
print("\\n found") // \n found
case "\r".utf8.first!:
print("\\r found") // \r found
default:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment