2

I want to split a string apple into substrings and want to store it in a array where I can get a at array[0] index p at array[1] and so on. Can anyone help me with that?

codercat
  • 22,159
  • 9
  • 58
  • 84

4 Answers4

3
NSMutableArray *stringBuffer = [NSMutableArray arrayWithCapacity:[string length]];
for (int i = 0; i < [string length]; i++) {
    [stringBuffer addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]];
}

// doing stuff with the array
Baby Groot
  • 4,617
  • 39
  • 52
  • 69
0

See: Convert NSString into char array

I don't know if the first of the 4 answers is the most efficient, but it works, is pretty clear in terms of how it's written and unless you've a particularly long string or high frequency of calls, is probably good enough.

Community
  • 1
  • 1
Andrew Hodgkinson
  • 3,771
  • 1
  • 29
  • 39
0

You can use

const char* x = [String UTF8String];

then access characters like x[0] x[1] and so on.

Islam Ahmed
  • 529
  • 5
  • 17
0

There are many ways to achieve this, few of them i am going to mention here:-

1)

NSMutableArray *yourArray = [NSMutableArray array];
for (int i = 0; i < [yourString length]; i++) {
    [yourArray addObject:[NSString stringWithFormat:@"%C", [yourString characterAtIndex:i]]];
}

2) you can see this answer https://stackoverflow.com/a/3581549/1865424.

There are other ways too.

Community
  • 1
  • 1
Kundan
  • 3,331
  • 2
  • 27
  • 65