替换NSString中每隔一秒引号(iOS NSString)

时间:2012-08-23 08:54:03

标签: objective-c ios nsstring

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

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

只是“一些”带有一些“更多”文字的文字

或在Objective-C中:

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

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

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

我无法找到替换每一秒或每第n次出现的方法,有人有提示/想法我是如何做到的吗?

非常感谢

4 个答案:

答案 0 :(得分: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 :(得分: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];
}

答案 2 :(得分: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);

答案 3 :(得分: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);