ios - 获取两个日期作为子字符串并将它们存储在单独的字符串中

标签 ios nsstring uikit nsdate

在我的应用程序中,我有一个字符串 - “比赛将从 dd/mm/yyyy 开始到 dd/mm/yyyy” 我想要将两个日期分开,所以我需要一种方法将这些日期存储在两个不同的字符串中。

最佳答案

您可以使用正则表达式来做到这一点(半公然地改编自 here )。

NSString *string                = @"contest will start from 10/11/2012 to 12/11/2012";

NSRegularExpression *regex      = [NSRegularExpression regularExpressionWithPattern:@"from (.*) to (.*)"
                                                                            options:0
                                                                              error:NULL];
NSTextCheckingResult *results   = [regex firstMatchInString:string
                                                    options:0
                                                      range:NSMakeRange(0, [string length])];
NSString *date1                 = [string substringWithRange:[results rangeAtIndex:1]];
NSString *date2                 = [string substringWithRange:[results rangeAtIndex:2]];

关于ios - 获取两个日期作为子字符串并将它们存储在单独的字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326350/

相关文章:

ios - "A GKScore must specify a leaderboard."

ios - 带有选项问题的 StringByReplacingOccurrencesOfString

iphone - NSString:plist 中的换行符转义

iphone - 不使用 OpenGL 以加法混合模式绘制图像

iphone - 当涉及高度动画的用户界面时,openGL ES 是否比 Core Animation 和 UIKit 具有更好的性能?

iOS 自定义信息窗口 Google Maps SDK 1.7.2

ios - HKTextview表情符号检测

ios - 如何将 NSString 转换为 Int?

cocoa-touch - 在 View 上显示UIBezierPath

iOS - 在应用程序卸载时清除应用程序数据