简体   繁体   中英

How do I get the date (not time or any alphabetic characters) from this NSString?

I have an NSString that prints:

recent update Last Updated: 10/02/2014 03:22:PM

I want to get this date only 10/02/2014 . How will I go about doing this? Any suggestions or tips are appreciated. I am guessing I need to use NSRange , but I am not too sure.

NSString *recentUpdate = [defaults stringForKey:@"EGORefreshTableView_LastRefresh"];

如果日期和月份始终为两位数,这是一种快速的解决方案:

[recentUpdate substringWithRange:NSMakeRange(28, 10)]

Use simple string parsing:

NSArray *stringParts = [recentUpdate componentsSeparatedBy:@" "];
NSString *datePortion = stringParts[4];

Do proper error checking incase the string doesn't have the right number of spaces.

just try.

    NSString *currentString = @"recent update Last Updated: 10/02/2014 03:22:PM";

    NSString *tempoString = [currentString substringWithRange:NSMakeRange(0, 39)];

    NSArray* stringArray = [tempoString componentsSeparatedByString: @":"];
    NSString* date = [stringArray objectAtIndex: 1];

    NSLog(@"%@", date);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM