将 NSString 转换为 char 数组
- 作者: 缺爱还是缺钙
- 来源: 51数据库
- 2022-10-25
问题描述
我有一个 char[] 类型的变量,我想在其中复制 NSString 值.如何将 NSString 转换为 char 数组?
I have a variable of type char[] and I want to copy NSString value in it. How can I convert an NSString to a char array?
推荐答案
使用-[NSString UTF8String]:
NSString *s = @"Some string"; const char *c = [s UTF8String];
您也可以使用 -[NSString cStringUsingEncoding:] 如果你的字符串是用非 UTF-8 编码的.
You could also use -[NSString cStringUsingEncoding:] if your string is encoded with something other than UTF-8.
一旦你有了 const char *,你就可以像使用 chars 数组一样使用它:
Once you have the const char *, you can work with it similarly to an array of chars:
printf("%c
", c[5]);
如果要修改字符串,请复制:
If you want to modify the string, make a copy:
char *cpy = calloc([s length]+1, 1); strncpy(cpy, c, [s length]); // Do stuff with cpy free(cpy);
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0
