将字符串拆分为数组的最佳方法
我正在开发一个旅行应用程序,我必须读取一个包含所有州和国家/地区的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 NSString 拆分为 NSArray 的最简单方法如下:
这应该适合您。你试过这个吗?
The easiest way to split a NSString into an NSArray is the following:
This should work for you. Have you tried this?
为了扩展 hspain 的答案,并使解决方案更符合问题,我向您提供以下内容,
这将为您提供一个 2 元素数组: [@"Zakinthos (ZTH)", @"GREECE"];
从这里您可以轻松到达该国。
并重复该过程以进一步细化:
stateArray 现在看起来像这样 [@"Zakinthos", @"(ZTH)"];
现在您已将国家/地区和州分成了自己的字符串,您可以使用它们构建新的数组,如下所示:
在处理位置文档时,您可以通过如下所示将更多国家/地区和州添加到其适当的数组中:
To expand on hspain's answer, and to bring the solution more in line with the question, i present you with the following
this gives you a 2 element array: [@"Zakinthos (ZTH)", @"GREECE"];
from here you can easily get the country.
and repeat the process to refine further:
stateArray now looks like this [@"Zakinthos", @"(ZTH)"];
and now you have Country and State separated into their own strings, which you can build new arrays with like this:
As you process your document of locations you can add more countries and states to their proper arrays via something like the following: