Ian
Ian

Reputation: 501

How to split newline from char pointer in Objective-C?

I have a buffer, that is char pointer. The content of this is something like:

aaa
bbb
ccc

How can I just get the aaa first line from that char pointer? I try to convert it into NSString first, but can I split it by \n in Objective-c?

Something like:

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

Upvotes: 2

Views: 1243

Answers (1)

Eugene
Eugene

Reputation: 10045

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

Upvotes: 6

Related Questions