Objective c 每隔一秒替换NSString(iOS NSString)中出现的引号

Objective c 每隔一秒替换NSString(iOS NSString)中出现的引号,objective-c,ios,nsstring,Objective C,Ios,Nsstring,我需要将已经存在的标准上双引号“”替换为德语文本的特定引号,以便在UIWebView中使用。 引用单词(每个偶数索引)前的第一个双引号字符需要替换为HTML等效的“which is”bdquo和被引用单词(每个单数索引)后的第二个,带有“which is“。引号可以在文本中的任何位置,因此我不能依赖任何固定位置 所以基本上我有一个如下的字符串: 只是“一些”文本和一些“更多”文本 或在Objective-C中: NSString *str = @"just \"some\" text

我需要将已经存在的标准上双引号“”替换为德语文本的特定引号,以便在UIWebView中使用。 引用单词(每个偶数索引)前的第一个双引号字符需要替换为HTML等效的“which is
”bdquo
和被引用单词(每个单数索引)后的第二个,带有“which is
“
。引号可以在文本中的任何位置,因此我不能依赖任何固定位置

所以基本上我有一个如下的字符串:

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

或在Objective-C中:

NSString *str = @"just \"some\" text with some \"more\" text";
我知道NSString中的stringByReplacingOccurrencesOfString方法,但当然这只会用底部的双引号替换所有双引号

NSString *newStr = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"„"];
我找不到一种方法来替换每一秒或每第n次出现的事件,有人有什么提示/想法吗


非常感谢

像这样的事情应该是朝着正确的方向推进:

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*stringbyreplacingeveryscondoccurrencewithstring(
NSString*常量pSource,
NSString*常量pSearch,
NSString*const pReplace)
{
/*@todo在复制之前测试pSource是否有两次出现,如果为false,则返回[pSource.copy autorelease]*/
NSMutableString*const str=[pSource.mutableCopy autorelease];
布尔-伊塞文=真;
对于(整数pos=0;pos

更新

如果您想按照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];
}
static NSString*StringByReplacingWithAlternatingStrings(
NSString*常量pSource,
NSString*常量pSearch,
NSString*常量预替换,
NSString*const pReplaceB)
{
/*@todo在复制之前测试pSource是否有两次出现,如果为false,则返回[pSource.copy autorelease]*/
NSMutableString*const str=[pSource.mutableCopy autorelease];
布尔-伊塞文=真;
对于(整数pos=0;pos
执行以下操作:

 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);
NSString*string=@“只是一些”文本和一些“更多”文本;
NSArray*arr=[字符串组件由字符串分隔:@“\”;
NSMutableString*formattedResponse=[NSMutableString];

对于(inti=0;i,我建议使用正则表达式

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);

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) ? @"&ldquo;" : @"&bdquo;")];
        quoteIndex++;
    }
}

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

这是相同的PHP问题+解决方案。您应该能够将其转换为objC。您可以将每个
替换为
谢谢,我选择了这个解决方案,因为它对我来说似乎是最简单的(我在“bdquo;”之前添加了一个缺少的符号)-works perfectbtw一个小的改进:如果您使用空字符串NSString*nextPart=@初始化NSString*nextPart;也可以在文本的第一个位置检测双引号,否则我的应用程序会冻结而不会出现错误代码。@hajn听起来不错。我已经添加了这一点,但如果您愿意,您可以随意编辑答案,并进行简单的改进。用户帮助优化答案是使其工作良好的原因之一。