4

我需要用德语文本的特定引号替换已经存在的标准上双引号“”(在我从数据库中获取的文本中),以便在 UIWebView 中使用。引用的单词(每个偶数索引)之前的第一个双引号字符需要替换为 HTML 等价物 „ which is „,引用的单词(每个奇数索引)之后的第二个双引号字符需要替换为 “ which is “。引号可以在文本中的任何位置,所以我不能依赖任何固定的位置。

所以基本上我有一个这样的 NSString:

只是“一些”文本和一些“更多”文本

或在 Objective-C 中:

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

我知道来自 NSString 的 stringByReplacingOccurrencesOfString 方法,但当然这只会用底部双引号替换所有双引号。

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

我找不到一种方法来替换每一秒或每第 n 次出现,有人有提示/想法我怎么能做到这一点?

非常感谢

4

4 回答 4

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);
于 2012-08-23T10:33:45.033 回答
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];
}

更新

如果您想按照 Caleb 对问题的编辑,您可以使用它来替换替换字符串:

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];
}
于 2012-08-23T09:50:26.570 回答
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);
于 2012-08-23T10:13:06.347 回答
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);
于 2012-08-23T10:05:15.880 回答