How to read from csv files

how to read large amount data in the csv files which are in the format of “123,123” , “345,345” , “567,567” , “789,789” and so on… and then to convert into array for the normal usage. where array can be in [“123,123”.“345,345”,“567,567”,“789,789”]

thanks for your help

Here’s a solution that uses NSRegularExpression:

let csv = """
"123,123" , "345,345" , "567,567"
"""

let regex = try NSRegularExpression(pattern: "\"[0-9,]+\"") // A quote, followed by a number (possibly with commas), followed by another quote
var values = [String]()

regex.enumerateMatches(in: csv, range: NSRange(csv.utf16.startIndex..<csv.utf16.endIndex, in: csv)) { result, _, _ in
	if let result = result {
		let match = (csv as NSString).substring(with: result.range)
		let value = String(match.dropFirst().dropLast()) // Drop the leading and trailing quotes
		values.append(value)
	}
}

I think NSRange requires the utf16 view, but I’d ask one of the String people to be sure. In a perfect world we wouldn’t have to deal with NSRange at all, but unfortunately it and NSRegularExpression are still around to make doing stuff like this annoying…