0

在我的应用程序中,我想按照以下步骤获取最终字符串。我的消息格式如下

message <br/><br/> On {date_time_stamp} sender_name wrote <br/><br/>

假设我的消息字符串如下

 Hello developers <br/><br/> On {2013-02-28 05:00:33} Bob wrote <br/><br/>
 Hello World <br/><br/> On {2013-02-27 08:35:33} Jack wrote <br/><br/>
 Hello Apple <br/><br/> On {2013-02-26 04:10:44} Tom wrote <br/><br/>

现在我需要按照以下步骤

  1. 一一获取消息中的所有日期

  2. 将其从 GMT 时区转换为本地时区

  3. 将消息中的 GMT 日期替换为本地日期时间

  4. 最后用 \n 替换“断线”并从消息中删除花括号。

我有以下代码

 -(void)parseMessageBody:(NSString*)msg
{
    NSRange openBracket = [msg rangeOfString:@"{"];
    NSRange closeBracket = [msg rangeOfString:@"}"];
    NSRange numberRange = NSMakeRange(openBracket.location + 1, closeBracket.location - openBracket.location - 1);
    NSString *numberString = [msg substringWithRange:numberRange];
    NSLog(@"Parsed string: %@",numberString);
}

-(NSString*)formattedMessageFromString:(NSString*)msg
{
    NSString *formattedMessage = @"";
    formattedMessage = [msg stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
    return formattedMessage;
}

-(NSString*)getLocalDateFromUTCDateString:(NSString*)utcDateString
{
    NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [serverDateFormatter setTimeZone:sourceTimeZone];
    [serverDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromServer = [serverDateFormatter dateFromString:utcDateString];
    DLog(@"UTC date From SERVER: %@",dateFromServer);

    NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
    [localDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* localTimeZone = [NSTimeZone systemTimeZone];
    [localDateFormatter setTimeZone:localTimeZone];
    NSString *localDate = [localDateFormatter stringFromDate:dateFromServer];
    DLog(@"Converted Local DAte : %@",localDate);

    return localDate;
}

我无法执行步骤 1 和 3 来获取消息中的所有日期并将其替换为本地日期时间。消息也会随着时间的推移而增长。所以解析需要很快。谁能告诉我如何获取消息中的所有日期并将其替换为转换后的日期?

任何形式的帮助都将受到高度赞赏。提前致谢。

4

1 回答 1

2

可以使用正则表达式查找和替换日期:

NSString *msg = @"Hello developers <br/><br/> On {2013-02-28 05:00:33} Bob wrote <br/><br/>\n"
   "Hello World <br/><br/> On {2013-02-27 08:35:33} Jack wrote <br/><br/>\n"
   "Hello Apple <br/><br/> On {2013-02-26 04:10:44} Tom wrote <br/><br/>\n";
NSMutableString *replacedMsg = [msg mutableCopy];

NSString *pattern = @"\\{(.+?)\\}"; // Pattern for { ... }
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];

__block int offset = 0;
[regex enumerateMatchesInString:msg options:0 range:NSMakeRange(0, [msg length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSRange range0 = [result range]; // range including { }
    NSRange range1 = [result rangeAtIndex:1]; // range excluding { }
    range0.location += offset;
    range1.location += offset;
    NSString *oldDate = [replacedMsg substringWithRange:range1];
    NSString *newDate = [self getLocalDateFromUTCDateString:oldDate];
    if (newDate != nil ) {
        [replacedMsg replaceCharactersInRange:range0 withString:newDate];
        offset += [newDate length] - range0.length;
    }
}];

NSLog(@"%@", replacedMsg);

输出(我的本地时区是 GMT+01):

Hello developers <br/><br/> On 2013-02-28 06:00:33 Bob wrote <br/><br/>
Hello World <br/><br/> On 2013-02-27 09:35:33 Jack wrote <br/><br/>
Hello Apple <br/><br/> On 2013-02-26 05:10:44 Tom wrote <br/><br/>
于 2013-04-20T10:46:51.973 回答