Guest User

Untitled

a guest
May 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. //
  2. // String+ReadLines.swift
  3. //
  4. // Created by Dmitry Shmidt on 5/16/18.
  5. //
  6.  
  7. import Foundation
  8.  
  9. extension URL{
  10. func readLines() throws -> [String] {
  11. let url = self
  12. let string = try String(contentsOf: url)
  13. let lines = string.splitByNewLines().compactMap{$0.trimmed()}
  14. return lines
  15. }
  16. }
  17.  
  18. extension String{
  19.  
  20. func splitByNewLines() -> [String]{
  21.  
  22. let newlineChars = CharacterSet.newlines
  23. let lines = components(separatedBy: newlineChars).filter{!$0.isEmpty}
  24. return lines
  25. }
  26.  
  27. func splitBySpace() -> [String]{
  28. let lines = self.split{$0 == " "}.map(String.init)
  29.  
  30. return lines
  31. }
  32.  
  33. func splitByCR() -> [String]{
  34. let lines = self.split { $0 == "\r" }.map(String.init)
  35. return lines
  36. }
  37.  
  38. func splitByNull() -> [String]{
  39. let lines = self.split { $0 == "\0"}.map(String.init)
  40. return lines
  41. }
  42.  
  43. func splitByWhitespacesAndNewlines() -> [String]{
  44. let newlineChars = CharacterSet.whitespacesAndNewlines
  45. let lines = components(separatedBy: newlineChars).filter{!$0.isEmpty}
  46. return lines
  47. }
  48.  
  49. func trimmed() -> String {
  50. let trimmedString = self.trimmingCharacters(in: .whitespaces)
  51. return trimmedString
  52. }
  53. }
Add Comment
Please, Sign In to add comment