Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #55 from hhoangnl/master
Browse files Browse the repository at this point in the history
Added overloaded ltrimmed and rtrimmed methods
  • Loading branch information
pNre committed Jan 3, 2015
2 parents ee6aff6 + 15e50b6 commit 7f29add
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions ExSwift/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ public extension String {
:returns: Stripped string
*/
func ltrimmed () -> String {
if let range = rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet) {
return ltrimmed(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}

/**
Strips the specified characters from the beginning of self.

:returns: Stripped string
*/
func ltrimmed (set: NSCharacterSet) -> String {
if let range = rangeOfCharacterFromSet(set.invertedSet) {
return self[range.startIndex..<endIndex]
}

Expand All @@ -150,7 +159,16 @@ public extension String {
:returns: Stripped string
*/
func rtrimmed () -> String {
if let range = rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet, options: NSStringCompareOptions.BackwardsSearch) {
return rtrimmed(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}

/**
Strips the specified characters from the end of self.

:returns: Stripped string
*/
func rtrimmed (set: NSCharacterSet) -> String {
if let range = rangeOfCharacterFromSet(set.invertedSet, options: NSStringCompareOptions.BackwardsSearch) {
return self[startIndex..<range.endIndex]
}

Expand Down
10 changes: 10 additions & 0 deletions ExSwiftTests/ExSwiftStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ class ExSwiftStringTests: XCTestCase {
XCTAssertEqual("AB ".rtrimmed(), "AB")
XCTAssertEqual("\n ABC ".rtrimmed(), "\n ABC")
}

func testLTrimmedForSet () {
XCTAssertEqual("ab ".ltrimmed(NSCharacterSet.alphanumericCharacterSet()), " ")
XCTAssertEqual(" ab".ltrimmed(NSCharacterSet.alphanumericCharacterSet()), " ab")
}

func testRTrimmedForSet () {
XCTAssertEqual("ab ".rtrimmed(NSCharacterSet.alphanumericCharacterSet()), "ab ")
XCTAssertEqual(" ab".rtrimmed(NSCharacterSet.alphanumericCharacterSet()), " ")
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ Name | Signature
**`matches`**|`matches (pattern: String, ignoreCase: Bool = false) -> [NSTextCheckingResult]?`
**`insert`**|`insert (index: Int, _ string: String) -> String`
**`ltrimmed`**|`ltrimmed () -> String`
**`ltrimmed`**|`ltrimmed (set: NSCharacterSet) -> String`
**`rtrimmed`**|`rtrimmed () -> String`
**`rtrimmed`**|`rtrimmed (set: NSCharacterSet) -> String`
**`trimmed`**|`trimmed () -> String`

#### Class Methods ####
Expand Down

0 comments on commit 7f29add

Please sign in to comment.