Skip to content

Instantly share code, notes, and snippets.

@superarts
Last active July 2, 2021 17:01
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 superarts/0bb264514c8c7cf0132bc654196497b6 to your computer and use it in GitHub Desktop.
Save superarts/0bb264514c8c7cf0132bc654196497b6 to your computer and use it in GitHub Desktop.
Swift String 1-Liners
// 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