简体   繁体   English

如何从此NSString获取日期(而不是时间或任何字母字符)?

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

I have an NSString that prints: 我有一个NSString打印:

recent update Last Updated: 10/02/2014 03:22:PM 最近更新最近更新:2014/10/02 03:22:PM

I want to get this date only 10/02/2014 . 我只想将此日期10/02/2014月2日。 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. 我猜我需要使用NSRange ,但是我不太确定。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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