Skip to content

Instantly share code, notes, and snippets.

@nuclearace
Last active October 22, 2019 09:55
Show Gist options
  • Select an option

  • Save nuclearace/dc30f939028b621abd1a9865b9e2f449 to your computer and use it in GitHub Desktop.

Select an option

Save nuclearace/dc30f939028b621abd1a9865b9e2f449 to your computer and use it in GitHub Desktop.
text between
import Foundation
public extension String {
func textBetween(_ startDelim: String, and endDelim: String) -> String {
precondition(!startDelim.isEmpty && !endDelim.isEmpty)
let startIdx: String.Index
let endIdx: String.Index
if startDelim == "start" {
startIdx = startIndex
} else if let r = range(of: startDelim) {
startIdx = r.upperBound
} else {
return ""
}
if endDelim == "end" {
endIdx = endIndex
} else if let r = self[startIdx...].range(of: endDelim) {
endIdx = r.lowerBound
} else {
endIdx = endIndex
}
return String(self[startIdx..<endIdx])
}
}
let tests = [
("Hello Rosetta Code world", "Hello ", " world"),
("Hello Rosetta Code world", "start", " world"),
("Hello Rosetta Code world", "Hello ", "end"),
("</div><div style=\"chinese\">你好嗎</div>", "<div style=\"chinese\">", "</div>"),
("<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">", "<text>", "<table>"),
("<table style=\"myTable\"><tr><td>hello world</td></tr></table>", "<table>", "</table>"),
("The quick brown fox jumps over the lazy other fox", "quick ", " fox"),
("One fish two fish red fish blue fish", "fish ", " red"),
("FooBarBazFooBuxQuux", "Foo", "Foo")
]
for (input, start, end) in tests {
print("Input: \"\(input)\"")
print("Start delimiter: \"\(start)\"")
print("End delimiter: \"\(end)\"")
print("Text between: \"\(input.textBetween(start, and: end))\"\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment