字符串到字符数组
我有这样的事情:
NSString *str = "hello this is 123.";
我希望将它放入一个字符数组中,这样我就可以一个字符一个字符地读取它。
所以它会像:
charArray[0] = 'h'
charArray[1] = 'e'
charArray[2] = 'l'
等等..
如何将字符串转换为字符数组以及如何读取字符数组的每个单元格?
I have something like:
NSString *str = "hello this is 123.";
I want it to be placed into a char array, so I can read it character by character.
So it would be like:
charArray[0] = 'h'
charArray[1] = 'e'
charArray[2] = 'l'
and so on..
How can I convert a string to a char array and how can I read each cell of the char array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的东西,请注意它是大写的 C,这真的让我对特殊字符感到困惑。
Try something like this, and note that it's a capital C, which really screwed me up with special characters.
如果您的字符串使用 UTF-8 以外的其他编码,您也可以使用 -[NSString cStringUsingEncoding:]。
You could also use -[NSString cStringUsingEncoding:] if your string is encoded with something other than UTF-8.
Nsstring 可能包含无法放入 char 的 utf 8 或 utf 16 字符,因此访问底层 char 数组可能不是一个好主意。
如果您不想,可以使用 characterAtIndex 消息来访问给定字符并迭代该字符串。
Nsstring may contain utf 8 or utf 16 character which can't fit in a char, so it may be a bad idea to access the underlying char array.
If you wan't you can use the characterAtIndex message to access a given character and iterate over the string.