Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 794
ARC Avatar asked May 21 '26 15:05

ARC


2 Answers

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
                                                  )
like image 109
zaph Avatar answered May 23 '26 13:05

zaph


How about

[fooString cStringUsingEncoding: NSUTF8StringEncoding];
like image 34
dasdom Avatar answered May 23 '26 11:05

dasdom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!