当前位置 : 主页 > 编程语言 > c语言 >

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

来源:互联网 收集:自由互联 发布时间:2021-06-05
NSString 与C++ string字符串的互转实例详解 1. string 转换为 NSString std::string str("hello"); NSString *str=[NSString stringWithString:str.c_str()]; NSString *istr=[NSString stringWithString:@"zsh"]; str=[istr cStringUsingEnco

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字符串的互转的实现代码,如有疑问请留言或者到社区讨论,谢 谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

网友评论