簡體   English   中英

如何從此NSString獲取日期(而不是時間或任何字母字符)?

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

我有一個NSString打印:

最近更新最近更新:2014/10/02 03:22:PM

我只想將此日期10/02/2014月2日。 我將如何去做? 任何建議或提示,不勝感激。 我猜我需要使用NSRange ,但是我不太確定。

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

如果日期和月份始終為兩位數,這是一種快速的解決方案:

[recentUpdate substringWithRange:NSMakeRange(28, 10)]

使用簡單的字符串解析:

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

如果字符串沒有正確的空格數,請進行適當的錯誤檢查。

你試一試。

    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