2012-08-23 77 views
4

我需要用UIWebView中用於德文文本的特定引號來替換已存在的標準上雙引號「」(在我從數據庫中獲取的文本中)。 引用單詞之前的第一個雙引號字符(每個偶數索引)需要用「這是„」的HTML等價物替換,並且引用單詞之後的第二個(每個奇數索引)用「which是“。引號可以在文本中的任何地方,所以我不能依賴任何固定的位置。替換NSString(iOS NSString)中引號的第二次出現

所以基本上我有這樣一個的NSString:

只是 「一些」 文字與一些 「更」 文本

或在Objective-C:

NSString *str = @"just \"some\" text with some \"more\" text"; 

我知道來自NSString的stringByReplacingOccurrencesOfString方法,但當然這隻會用底部雙引號標記替換所有雙引號。

NSString *newStr = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"„"]; 

我不能找到一種方法,只需更換每秒或每n次出現,是否有人有一個提示/想法如何,我可以做到嗎?

非常感謝

+1

這裏有同樣的問題+解決方案PHP http://stackoverflow.com/questions/6224893/replace-english-quotes-to-german-quotes 。你應該可以把它翻譯成objC。 –

+1

你可以用'''替換每個''' – Felix

回答

2

NSScanner提供了另一種選擇......

NSScanner *scanner = [NSScanner scannerWithString:englishString]; 
NSMutableString *germanString = [NSMutableString string]; 
BOOL foundQuote = YES; 
int quoteIndex = 0; 

while (foundQuote) { 
    NSString *nextPart = @""; 
    [scanner scanUpToString:@"\"" intoString:&nextPart]; 
    if (nextPart != nil) { 
     [germanString appendString:nextPart]; 
    } 
    foundQuote = [scanner scanString:@"\"" intoString:nil]; 
    if (foundQuote) { 
     [germanString appendString:((quoteIndex % 2) ? @"“" : @"„")]; 
     quoteIndex++; 
    } 
} 

NSLog(@"The German version is: %@", germanString); 
+1

謝謝,我用這個解決方案,因爲它對我來說似乎是最直接的(我在「bdquo;」之前添加了缺失&符號) - 完美的作品 – hajn

+0

很高興幫助並感謝指出缺少&) – Caleb

+0

btw一個小改進:如果您初始化NSString * nextPart;與一個空字符串NSString * nextPart = @「」;它也可能檢測到雙引號在文本的第一個位置,否則我的應用程序會凍結而不會出現錯誤代碼.. – hajn

1

這樣的事情應該是在正確的方向一推:

static NSString * StringByReplacingEverySecondOccurrenceWithString(
         NSString * const pSource, 
         NSString * const pSearch, 
         NSString * const pReplace) 
{ 
    /* @todo test that pSource has two occurrences before copying, and return [pSource.copy autorelease] if false. */ 
    NSMutableString * const str = [pSource.mutableCopy autorelease]; 
    bool isEven = true; 
    for (NSUInteger pos = 0; pos < str.length; isEven = !isEven) { 
    const NSRange remainder = NSMakeRange(pos, str.length - pos); 
    const NSRange next = [str rangeOfString:pSearch options:0 range:remainder]; 
    if (NSNotFound != next.location && !isEven) { 
     [str replaceCharactersInRange:next withString:pReplace]; 
    } 
    pos = next.location + next.length; 
    } 
    return [str.copy autorelease]; 
} 

更新

,如果你想跟着迦勒的編輯到問題,你可以用它來交替替換字符串:

static NSString * StringByReplacingWithAlternatingStrings(
          NSString * const pSource, 
          NSString * const pSearch, 
          NSString * const pReplaceA, 
          NSString * const pReplaceB) 
{ 
    /* @todo test that pSource has two occurrences before copying, and return [pSource.copy autorelease] if false. */ 
    NSMutableString * const str = [pSource.mutableCopy autorelease]; 
    bool isEven = true; 
    for (NSUInteger pos = 0; pos < str.length; isEven = !isEven) { 
    const NSRange remainder = NSMakeRange(pos, str.length - pos); 
    const NSRange next = [str rangeOfString:pSearch options:0 range:remainder]; 
    if (NSNotFound != next.location) { 
     NSString * const substitution = isEven ? pReplaceA : pReplaceB; 
     [str replaceCharactersInRange:next withString:substitution]; 
     pos = next.location + substitution.length; 
    } 
    else { 
     pos = NSNotFound; 
    } 
    } 
    return [str.copy autorelease]; 
} 
0

這樣做:

NSString *string = @"just \"some\" text with some \"more\" text"; 
NSArray *arr = [string componentsSeparatedByString:@"\""]; 
NSMutableString *formattedResponse = [NSMutableString string]; 
for (int i = 0;i<[arr count];i++)//(NSString *str in arr) 
{ 
    if(i == 0) 
    { 
     [formattedResponse appendString:[arr objectAtIndex:i]]; 
    } 
    else{ 
    if(i%2 == 0) 
    { 
     [formattedResponse appendString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]]]; 
    } 
    else{ 
     [formattedResponse appendString:[NSString stringWithFormat:@"\"%@,,",[arr objectAtIndex:i]]]; // replace downward quotes as i don't know 
    } 
    } 
} 
NSLog(@"formattedResponse : %@",formattedResponse); 
1

我會建議使用正則表達式即可。

NSString *str = @"just \"some\" text with some \"more\" text"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\"(\\w+)\"" options:NSRegularExpressionCaseInsensitive error:nil]; 
NSString *returnString = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:@"\"$1&quote"]; 
NSLog(@"%@",returnString);