Last active
August 29, 2015 14:23
-
-
Save matthewcheok/4ea0a89d674db96ff059 to your computer and use it in GitHub Desktop.
String Utilities
This file contains 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
extension Character { | |
public static var newlineCharacterSet: Set<Character> { | |
return ["\u{000A}", "\u{000B}", "\u{000C}", "\u{000D}", "\u{0085}"] | |
} | |
public static var whiteSpaceCharacterSet: Set<Character> { | |
return ["\u{0020}", "\u{0009}"] | |
} | |
public static var whiteSpaceAndNewlineCharacterSet: Set<Character> { | |
return self.newlineCharacterSet.union(self.whiteSpaceCharacterSet) | |
} | |
} | |
extension String { | |
public func trimCharactersInSet(characterSet: Set<Character>) -> String { | |
var startIndex = self.startIndex | |
var endIndex = self.endIndex | |
while startIndex < endIndex && characterSet.contains(self[startIndex]) { | |
startIndex = startIndex.successor() | |
} | |
while endIndex > startIndex && characterSet.contains(self[endIndex.predecessor()]) { | |
endIndex = endIndex.predecessor() | |
} | |
return self[startIndex..<endIndex] | |
} | |
public func trimWhiteSpaceAndNewline() -> String { | |
return trimCharactersInSet(Character.whiteSpaceAndNewlineCharacterSet) | |
} | |
} | |
public func +(lhs: String.CharacterView.Index, rhs: Int) -> String.CharacterView.Index { | |
return advance(lhs, rhs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment