ARC
ARC

Reputation: 1804

Extract characters from NSString object

How to extract individual characters from a string object in Objective-C ?

Example:

NSString * fooString = [NSString stringWithFormat:@"FOOSTRING"];

I would like to extract individual characters from string object fooString is pointing to. F , O, O , S, T, R, I, N, G.

Upvotes: 2

Views: 4888

Answers (3)

amar
amar

Reputation: 4345

you can also use [NSString getCharacters:(unichar array) range:(nsrange)]; you will get an easy to manipulate array.I needed it my self and found it on this link

Upvotes: 1

zaph
zaph

Reputation: 112857

NSString * fooString = @"FOOSTRING";
NSMutableArray *list = [NSMutableArray array];
for (int i=0; i<fooString.length; i++) {
    [list addObject:[fooString substringWithRange:NSMakeRange(i, 1)]];
}
NSLog(@"%@", list);

NSLog output:

(
                                                  F,
                                                  O,
                                                  O,
                                                  S,
                                                  T,
                                                  R,
                                                  I,
                                                  N,
                                                  G
                                                  )

Upvotes: 10

dasdom
dasdom

Reputation: 14063

How about

[fooString cStringUsingEncoding: NSUTF8StringEncoding];

Upvotes: 5

Related Questions