将字符串拆分为数组的最佳方法

发布于 2024-11-27 11:31:10 字数 366 浏览 2 评论 0原文

我正在开发一个旅行应用程序,我必须读取一个包含所有州和国家/地区的txt文件,因为您可以注意到这是一个相当大的文件来读取,无论如何,这是文件内文本的示例:

Zakinthos (ZTH),希腊
曾斯维尔 (ZZV),美国
伊朗赞詹 (JWN)
坦桑尼亚桑给巴尔 (ZNZ)
...

现在我正在毫无问题地读取文件,但我不知道如何创建两个数组,一个包含州,另一个包含国家,这样我可以在自动完成文本时将它们显示到表格中。

我尝试使用 NSScanner 使用“,”作为分隔符,但我不知道如何使其有效,而且我尝试使用 NSString 方法但没有结果。

欢迎任何帮助,提前谢谢!

顺便说一句,对不起我的英语 XD

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-12-04 11:31:10

将 NSString 拆分为 NSArray 的最简单方法如下:

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

这应该适合您。你试过这个吗?

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?

南…巷孤猫 2024-12-04 11:31:10

为了扩展 hspain 的答案,并使解决方案更符合问题,我向您提供以下内容,

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

这将为您提供一个 2 元素数组: [@"Zakinthos (ZTH)", @"GREECE"];

从这里您可以轻松到达该国。

NSString *countryString = stringArray[1]; // GREECE

并重复该过程以进一步细化:

NSArray *stateArray = [(NSString*)stringArray[0] componentsSeparatedByString:@" "];

stateArray 现在看起来像这样 [@"Zakinthos", @"(ZTH)"];

NSString *stateString = stateArray[0]; // Zakinthos

现在您已将国家/地区和州分成了自己的字符串,您可以使用它们构建新的数组,如下所示:

NSMutableArray *countryArray = [NSMutableArray arrayWithObjects:countryString, nil];
NSMutableArray *stateArray = [NSMutableArray arrayWithObjects:stateString, nil];

在处理位置文档时,您可以通过如下所示将更多国家/地区和州添加到其适当的数组中:

[countryArray addObject:newCountryString];
[stateArray addObject:newStateString];

To expand on hspain's answer, and to bring the solution more in line with the question, i present you with the following

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

this gives you a 2 element array: [@"Zakinthos (ZTH)", @"GREECE"];

from here you can easily get the country.

NSString *countryString = stringArray[1]; // GREECE

and repeat the process to refine further:

NSArray *stateArray = [(NSString*)stringArray[0] componentsSeparatedByString:@" "];

stateArray now looks like this [@"Zakinthos", @"(ZTH)"];

NSString *stateString = stateArray[0]; // Zakinthos

and now you have Country and State separated into their own strings, which you can build new arrays with like this:

NSMutableArray *countryArray = [NSMutableArray arrayWithObjects:countryString, nil];
NSMutableArray *stateArray = [NSMutableArray arrayWithObjects:stateString, nil];

As you process your document of locations you can add more countries and states to their proper arrays via something like the following:

[countryArray addObject:newCountryString];
[stateArray addObject:newStateString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文