Last active
July 2, 2021 17:01
-
-
Save superarts/0bb264514c8c7cf0132bc654196497b6 to your computer and use it in GitHub Desktop.
Swift String 1-Liners
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
// string to array: https://www.hackingwithswift.com/example-code/strings/how-to-split-a-string-into-an-array-componentsseparatedby | |
str.components(separatedBy: ",") | |
// remove leading and trailing: https://www.hackingwithswift.com/example-code/strings/how-to-trim-whitespace-in-a-string | |
str.trimmingCharacters(in: .whitespacesAndNewlines) | |
// remove blank lines: https://stackoverflow.com/questions/35897807/how-should-i-remove-all-the-empty-lines-from-a-string | |
// split returns [Substring] which is faster. | |
str.split(whereSeparator: \.isNewline).joined(separator: "\n") | |
// 2 ways contains: https://www.hackingwithswift.com/example-code/strings/how-to-check-whether-a-string-contains-any-words-from-an-array | |
let combinedResult = words.contains(where: string.contains) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment