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?
Asked
Active
Viewed 125 times
2
-
If you just want to access the chars in the string, then characterAtIndex is enough. – zdd Mar 18 '14 at 05:48
-
I think he has explained it well..http://stackoverflow.com/a/3581549/1865424 – Kundan Mar 18 '14 at 05:49
4 Answers
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,637
- 39
- 52
- 71
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
- 4,379
- 3
- 33
- 43
0
You can use
const char* x = [String UTF8String];
then access characters like x[0] x[1] and so on.

Islam Ahmed
- 539
- 1
- 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.