it would create a little workaround here, URLComponents can help to you in this case as your input seems a standard query string to me (see RFC-3986).
let result1: String = "t=20171017T1201&s=349.00&fn=8712000100030779&i=21456&fp=124519970&n=1"
so I would take advantages of that and expect working with an array of URLQueryItem, so this extension has been placed on an array (maybe not effective but quite straightforward).
extension Array where Iterator.Element == URLQueryItem {
subscript(key: String) -> String? {
get {
return self.filter { $0.name == key }.first?.value
}
}
}
then I would just read the components from a randomly generated but syntactically valid URL in any random order:
if let url = URL(string: "u://?\(result1)"),
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
// ...
let t: String? = urlComponents.queryItems?["t"] // "20171017T1201"
let s: String? = urlComponents.queryItems?["s"] // "349.00"
let fp: String? = urlComponents.queryItems?["fp"] // "124519970"
let random: String? = urlComponents.queryItems?["random"] // nil
// ...
}
NOTE: this solution is based on the nature of your input string (query-string), if the input format changes in future, you may need to use regular expressions directly to extract information.