Extract characters from NSString object

后端 未结 3 1799
独厮守ぢ
独厮守ぢ 2021-01-22 08:05

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

Example:

NSString * fooString = [NSString stringWithFormat:@         


        
相关标签:
3条回答
  • 2021-01-22 08:50
    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
                                                      )
    
    0 讨论(0)
  • 2021-01-22 08:51

    How about

    [fooString cStringUsingEncoding: NSUTF8StringEncoding];
    
    0 讨论(0)
  • 2021-01-22 09:02

    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

    0 讨论(0)
提交回复
热议问题