Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to split strings into an array

Tags:

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

like image 252
Rafael Jimeno Avatar asked Aug 02 '11 22:08

Rafael Jimeno


People also ask

How do I split a string into multiple parts?

As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.

Does .split work on arrays?

The split( ) method doesn't work directly for arrays. However, we can first convert the elements of our array to a string, then we can use the split( ) method.

How do you split a string into an array in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

What divides a string into an array of substrings?

The split() method splits (divides) a string into two or more substrings depending on a splitter (or divider). The splitter can be a single character, another string, or a regular expression. After splitting the string into multiple substrings, the split() method puts them in an array and returns it.


1 Answers

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?

like image 166
hspain Avatar answered Oct 13 '22 00:10

hspain