从复杂的NSString中提取日期 [英] Extract a date from a complex NSString

查看:77
本文介绍了从复杂的NSString中提取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode中没有能力解决此问题:

I don't have the ability in Xcode to resolve this problem:

我有这段文本:

402
加西亚
15年8月1日10:26
Observaciones delhuésped

"402 Garcia 01/08/15 10:26 Observaciones del huésped"

我想提取我确定的日期是格林尼治标准时间+0,然后添加电话格林尼治标准时间(例如GMT +1),并将旧日期替换为NSString中的新日期。

And i want to extract the date that i'm sure is GMT +0, then add the phone GMT for example GMT +1 and the replace that old date with the new date inside the NSString.

我刚刚在其他地方解决过格林尼治标准时间的东西,所以我只需要提取日期字符串并将其替换为字符串,这样我的最终结果将是:

The GMT stuff i have just solve it in another place so i just need to extract and replace the date string in to the string so my final result will be something like:

402
加西亚
15/08/15 11:26
Observaciones delhuésped

"402 Garcia 01/08/15 11:26 Observaciones del huésped"

任何帮助将不胜感激,并感谢

Any help will be appreciated and thanks in advance.

推荐答案

这正是NSDataDetector 就是那里。

我在NSString上的类别中创建了一个方法:

I made a method in a category on NSString:

@interface NSString (HASAdditions)

- (NSArray *)detectedDates;

@end


@implementation NSString (HASAdditions)

- (NSArray *)detectedDates {
    NSError *error = nil;
    NSDataDetector *dateDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
    if (!dateDetector) return nil;
    NSArray *matches = [dateDetector matchesInString:self options:kNilOptions range:NSMakeRange(0, self.length)];
    NSMutableArray *dates = [[NSMutableArray alloc] init];
    for (NSTextCheckingResult *match in matches) {
        if (match.resultType == NSTextCheckingTypeDate) {
            [dates addObject:match.date];
        }
    }
    return dates.count ? [dates copy] : nil;
}

您可以这样称呼它:

NSArray *dates = [@"402 Garcia 01/08/15 10:26 Observaciones del huésped" detectedDates];

您可以在 NSHipster

这篇关于从复杂的NSString中提取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆