Skip to content

Instantly share code, notes, and snippets.

Created August 24, 2014 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1d0f2bbb397596f88664 to your computer and use it in GitHub Desktop.
Save anonymous/1d0f2bbb397596f88664 to your computer and use it in GitHub Desktop.
import Foundation
extension String {
func split(separator:String) -> [String] {
return self.componentsSeparatedByString(separator)
}
func split(separator:String) -> (String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 2) ? (pieces[0], pieces[1]) : nil
}
func split(separator:String) -> (String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 3) ? (pieces[0], pieces[1], pieces[2]) : nil
}
func split(separator:String) -> (String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 4) ? (pieces[0], pieces[1], pieces[2], pieces[3]) : nil
}
func split(separator:String) -> (String, String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 5) ? (pieces[0], pieces[1], pieces[2], pieces[3], pieces[4]) : nil
}
func split(separator:String) -> (String, String, String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 6) ? (pieces[0], pieces[1], pieces[2], pieces[3], pieces[4], pieces[5]) : nil
}
func split(separator:String) -> (String, String, String, String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 7) ? (pieces[0], pieces[1], pieces[2], pieces[3], pieces[4], pieces[5], pieces[6]) : nil
}
func split(separator:String) -> (String, String, String, String, String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 8) ? (pieces[0], pieces[1], pieces[2], pieces[3], pieces[4], pieces[5], pieces[6], pieces[7]) : nil
}
func split(separator:String) -> (String, String, String, String, String, String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 9) ? (pieces[0], pieces[1], pieces[2], pieces[3], pieces[4], pieces[5], pieces[6], pieces[7], pieces[8]) : nil
}
func split(separator:String) -> (String, String, String, String, String, String, String, String, String, String)? {
let pieces:[String] = self.split(separator)
return (pieces.count == 10) ? (pieces[0], pieces[1], pieces[2], pieces[3], pieces[4], pieces[5], pieces[6], pieces[7], pieces[8], pieces[9]) : nil
}
}
if let (hello, world) = "Hello, world".split(", ") {
println(hello)
println(world)
}
if let (hello, world) = "Hello, world, omg".split(", ") {
println(hello)
println(world)
}
if let (first, last, birthday) = "Bob, Quone, January 1st 1987".split(", ") {
println(first)
println(last)
println(birthday)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment