How to get a single NSString character from an NSString

前端 未结 4 1547
庸人自扰
庸人自扰 2021-01-30 15:56

I want to get a character from somewhere inside an NSString. I want the result to be an NSString.

This is the code I use to get a single character at index it:



        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 16:27

    If you just want to get one character from an a NSString, you can try this.

    - (unichar)characterAtIndex:(NSUInteger)index;
    

    Used like so:

    NSString *originalString = @"hello";
    int index = 2;
    NSString *theCharacter = [NSString stringWithFormat:@"%c", [originalString characterAtIndex:index-1]];
    //returns "e".
    

提交回复
热议问题