2013-02-28 61 views
0

在我的應用程序中,我想按照以下步驟獲取最終化字符串。 我的郵件格式如下解析大字符串並用iOS 6+中的新值替換解析的字符串值

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時區轉換爲本地l分時區

  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得到的所有日期的消息&與當地的日期及時更換。 此消息可隨時間增長。所以解析需要很快。 任何人都可以告訴我如何獲得消息&中的所有日期替換爲轉換日期?

任何形式的幫助,高度讚賞。提前致謝。

回答

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/> 
+0

讓我知道如果你需要更多的解釋正則表達式! – 2013-04-20 10:47:41

+0

作品正是我正在尋找的!萬分感謝。 – iOSAppDev 2013-04-20 12:18:22

+0

你能告訴我如何將HTML代碼中的'li'標籤替換爲數字嗎?這是我的問題http://stackoverflow.com/questions/16207919/repalce-ol-li-tags-from-html-code-with-number-in-ios-6 – iOSAppDev 2013-04-25 06:35:06