解析大字符串&用iOS 6+中的新值替换已解析的字符串值

时间:2013-02-28 13:03:42

标签: ios6 replace string-parsing

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

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. 最后将“breakline with \ n”替换为&amp;从消息中删除花括号。

  5. 我有以下代码

     -(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步和第1步。 3获取消息中的所有日期&amp;用当地日期时间替换它。 消息也可以随着时间的推移而增长。因此解析需要快速。 任何人都可以告诉我如何在消息中获取所有日期&amp;用转换后的日期替换它?

    非常感谢任何形式的帮助。提前谢谢。

1 个答案:

答案 0 :(得分: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/>