详解NSString 与C++ string字符串的互转

时间:2021-09-28 06:10:01

NSStringC++ string字符串的互转实例详解

1.  string 转换为 NSString 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string str("hello");
NSString *str=[NSString stringWithString:str.c_str()];
 
NSString *istr=[NSString stringWithString:@"zsh"];
 
 str=[istr cStringUsingEncoding: NSUTF8StringEncoding];
 NSString *path= [[NSBundle mainBundle]pathForResource:@"nokia" ofType:@"mp3"];
 
 char *tempPath=(char*)malloc(sizeof(path)+1);
  
 NSString *soundPath=[[NSBundle mainBundle] pathForResource:@mp3"]; 
  
 NSString* dirStr = [NSString stringWithFormat:@"%s",path];
 NSString* filepath = [[NSBundle mainBundle] pathForResource:dirStr ofType:nil];
 NSLog(filepath);

 2.  //NSString 转换为C 字符串

?
1
2
3
const char* destDir = [filepath UTF8String];
 
/ NSURL *soundUrl=[[NSURL alloc] initFileURLWithPath:s]; 

3.const char * to NSString

?
1
2
char *str ="abc";
SString *s = [NSString stringWithUTF8String:str];

以上就是NSString 与C++ string字符串的互转的实现代码,如有疑问请留言或者到社区讨论,谢 谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/typename/article/details/6730183