Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // String+ReadLines.swift
- //
- // Created by Dmitry Shmidt on 5/16/18.
- //
- import Foundation
- extension URL{
- func readLines() throws -> [String] {
- let url = self
- let string = try String(contentsOf: url)
- let lines = string.splitByNewLines().compactMap{$0.trimmed()}
- return lines
- }
- }
- extension String{
- func splitByNewLines() -> [String]{
- let newlineChars = CharacterSet.newlines
- let lines = components(separatedBy: newlineChars).filter{!$0.isEmpty}
- return lines
- }
- func splitBySpace() -> [String]{
- let lines = self.split{$0 == " "}.map(String.init)
- return lines
- }
- func splitByCR() -> [String]{
- let lines = self.split { $0 == "\r" }.map(String.init)
- return lines
- }
- func splitByNull() -> [String]{
- let lines = self.split { $0 == "\0"}.map(String.init)
- return lines
- }
- func splitByWhitespacesAndNewlines() -> [String]{
- let newlineChars = CharacterSet.whitespacesAndNewlines
- let lines = components(separatedBy: newlineChars).filter{!$0.isEmpty}
- return lines
- }
- func trimmed() -> String {
- let trimmedString = self.trimmingCharacters(in: .whitespaces)
- return trimmedString
- }
- }
Add Comment
Please, Sign In to add comment