Split an NSString to access one particular piece

后端 未结 7 1585
轮回少年
轮回少年 2020-11-27 11:59

I have a string like this: @\"10/04/2011\" and I want to save only the \"10\" in another string. How can I do that?

相关标签:
7条回答
  • 2020-11-27 12:50

    Either of these 2:

    NSString *subString = [dateString subStringWithRange:NSMakeRange(0,2)];
    NSString *subString = [[dateString componentsSeparatedByString:@"/"] objectAtIndex:0];
    

    Though keep in mind that sometimes a date string is not formatted properly and a day ( or a month for that matter ) is shown as 8, rather than 08 so the first one might be the worst of the 2 solutions.

    The latter should be put into a separate array so you can actually check for the length of the thing returned, so you do not get any exceptions thrown in the case of a corrupt or invalid date string from whatever source you have.

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