Skip to content

Instantly share code, notes, and snippets.

@reitzig
Last active April 7, 2017 12:54
Show Gist options
  • Save reitzig/a4eed86b654cbd38e21fc90579ee5d10 to your computer and use it in GitHub Desktop.
Save reitzig/a4eed86b654cbd38e21fc90579ee5d10 to your computer and use it in GitHub Desktop.
String prefix and suffix for Swift 3.0
import Foundation
extension String {
/**
Drops characters from the front and the end of this string, returning
what remains.
- Parameter first: The number of characters to drop from the beginning.
Values smaller than 0 are treated as 0.
- Parameter last: The number of characters to drop from the end.
Values smaller than 0 are treated as 0.
*/
func dropping(first: Int = 0, last: Int = 0) -> String {
let from = self.index(self.startIndex, offsetBy: max(first, 0),
limitedBy: self.endIndex)!
let to = self.index(self.endIndex, offsetBy: -max(last, 0),
limitedBy: self.startIndex)!
if to < from {
return ""
} else {
return self.substring(with: from..<to)
}
}
/**
- Parameter first: The number of characters to take.
Values smaller than 0 are treated as 0.
- Returns: the string consisting of the first `first` characters of this string.
If `first` is larger than the total number of characters, the whole
string is returned.
*/
func taking(first: Int) -> String {
if first <= 0 {
return ""
} else if let to = self.index(self.startIndex, offsetBy: first, limitedBy: self.endIndex) {
return self.substring(to: to)
} else {
return self
}
}
/**
- Parameter last: The number of characters to take.
Values smaller than 0 are treated as 0.
- Returns: the string consisting of the last `take` characters of this string.
If `last` is larger than the total number of characters, the whole
string is returned.
*/
func taking(last: Int) -> String {
if last <= 0 {
return ""
} else if let from = self.index(self.endIndex, offsetBy: -last, limitedBy: self.startIndex) {
return self.substring(from: from)
} else {
return self
}
}
}
import Foundation
import XCTest
class StringCutterTests: XCTestCase {
func testDrop() {
XCTAssertEqual("monkey", "monkey".dropping(first: 0, last: 0))
XCTAssertEqual("monk", "monkey".dropping(first: 0, last: 2))
XCTAssertEqual("nkey", "monkey".dropping(first: 2, last: 0))
XCTAssertEqual("nk", "monkey".dropping(first: 2, last: 2))
XCTAssertEqual("", "monkey".dropping(first: 3, last: 3))
XCTAssertEqual("", "monkey".dropping(first: 4, last: 4))
XCTAssertEqual("monke", "monkey".dropping(first: -1, last: 1))
XCTAssertEqual("onkey", "monkey".dropping(first: 1, last: -1))
XCTAssertEqual("monkey", "monkey".dropping(first: -1, last: -1))
}
func testPrefix() {
XCTAssertEqual("", "monkey".taking(first: -1))
XCTAssertEqual("", "monkey".taking(first: 0))
XCTAssertEqual("m", "monkey".taking(first: 1))
XCTAssertEqual("mon", "monkey".taking(first: 3))
XCTAssertEqual("monkey", "monkey".taking(first: 7))
}
func testSuffix() {
XCTAssertEqual("", "monkey".taking(last: -1))
XCTAssertEqual("", "monkey".taking(last: 0))
XCTAssertEqual("y", "monkey".taking(last: 1))
XCTAssertEqual("key", "monkey".taking(last: 3))
XCTAssertEqual("monkey", "monkey".taking(last: 7))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment