Replace every second occurrence of quotes in NSString (iOS NSString)

I need to replace the standard upper double quotes "" that already exist (in the text I get from the database), with specific quotes for German text for use in UIWebView. The first double-quote character before the quoted word (s) (each even index) must be replaced by the HTML equivalent "which is „ and the second after the quoted word (s) (each odd index) which is “ ;. The quotation marks can be anywhere text, so I cannot rely on any fixed positions.

So basically I have NSString:

just "some" text with the text "more"

or Objective-C:

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

I am aware of the stringByReplacingOccurrencesOfString method from NSString, but of course this replaces all double quotes with a lower double quotation mark.

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

I could not find a way to simply replace every second or every nth case, does anyone have a clue / idea, how could I do this?

thanks a lot

+4
source share
4 answers

NSScanner offers another option ...

 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); 
+2
source

something like this should be a push in the right direction:

 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]; } 

Update

and if you want to follow the caleb rule of the question, you can use it to alternate replaced strings:

 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]; } 
+1
source

I would suggest using regular expressions.

 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); 
+1
source

Do it:

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

All Articles