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

NSString 与C++ string字符串的互转实例详解 1. string 转换为 NSString std::string str(“hello”); NSString *str=[NSString stringWithString:str.c_str()]; NSString *istr=[NSString stringWithString:@”zsh”]; str=[istr cStringUsingEncoding: NSUTF

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

1.  string 转换为 NSString 

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 字符串

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

3.const char * to NSString

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

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

关于作者: dawei

【声明】:石家庄站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐