Last active
July 10, 2021 20:51
-
-
Save krzyzanowskim/5ae4baaa2b01419227f7d943f01f400f to your computer and use it in GitHub Desktop.
In Swift \r\n is a single Character, just like \n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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