如何在Objective-C中从char指针拆分换行符?

时间:2011-12-23 09:05:23

标签: objective-c nsstring split

我有一个缓冲区,即char指针。这个内容类似于:

aaa
bbb
ccc

如何从aaa指针获取char第一行? 我首先尝试将其转换为NSString,但我可以在Objective-c中将其拆分为\n吗?

类似的东西:

arrayString[0] = "aaa"arrayString[1] = "bbb"arrayString[2] = "ccc"

1 个答案:

答案 0 :(得分:6)

  char *buffer = "aaa \n bbb \n ccc \n";
  NSString *str = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
  NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
  NSLog(@"arr: %@", arr);