Best way to split strings into an array

Rafael Jimeno picture Rafael Jimeno · Aug 3, 2011 · Viewed 34.9k times · Source

I'm developing a travel app, I have to read a txt file that contains all the states and countries, as you can notice this is a pretty huge file to read, anyway, this is an example of the text inside the file:

Zakinthos (ZTH),GREECE
Zanesville (ZZV),USA
Zanjan (JWN),IRAN
Zanzibar (ZNZ),TANZANIA
...

Now i'm reading the file with no problem but I don't know how to create two arrays, one with the state and another with the country so I can show them into a table when text is being autocompleted.

I've tried using NSScanner using the "," as a delimiter but I don't know how to make that works and also I tried with NSString methods with no results.

Any help is welcomed, Thank you in advance!!!.

btw sorry about my english XD

Answer

hspain picture hspain · Aug 3, 2011

The easiest way to split a NSString into an NSArray is the following:

NSString *string = @"Zakinthos (ZTH),GREECE";
NSArray *stringArray = [string componentsSeparatedByString: @","];

This should work for you. Have you tried this?