我有一个 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);
这篇关于将 NSString 转换为 char 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何使用或不使用 ScrollView 使 ImageView 可缩放?how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可缩放?)
如何在 iphone 应用程序开发中的导航栏上添加 1How can i add more than 10 buttons on a navigationbar in iphone application development?(如何在 iphone 应用程序开发中的导航栏上添加 10
UIScrollView 只能用一根手指滚动UIScrollView scrolling only with one finger(UIScrollView 只能用一根手指滚动)
在 UIScrollView 中使用 UITouchUsing UITouch inside a UIScrollView(在 UIScrollView 中使用 UITouch)
UitextField resignFirstResponder 在滚动视图中不起作用UitextField resignFirstResponder does not work in scroll view(UitextField resignFirstResponder 在滚动视图中不起作用)
如何阻止 UIScrollView 吞下触摸How to Stop a UIScrollView from Swallowing Touches(如何阻止 UIScrollView 吞下触摸)