2012-03-08 11 views

Odpowiedz

12

Jak o

// find first occurrence of search string in source string 
NSRange range = [sourceString rangeOfString:@"searchString"]; 
while(range.location != NSNotFound) 
{ 
    // build a new string with your changed values 

    range = [sourceString rangeOfString:@"searchString" options:0 range:NSMakeRange(range.location + 1, [sourceString length] - range.location - 1)]; 
} 

Albo po prostu

[sourceString stringByReplacingOccurrencesOfString:searchString withString:targetString]; 

jeśli chcesz zmienić searchString na tę samą wartość wszędzie w ciągu źródłowego.

+0

Również używając: NSArray * stringArray = [myString componentsSeparatedByString: @ "mySubString"]; –

+0

Stuknęłam w niewłaściwy klucz i moja odpowiedź została początkowo przesłana jako niekompletna :-) – TheEye

+2

Według składnikówSeparated ... usuwasz wszystkie wystąpienia szukanego ciągu, nie robisz ich iteracji ... – TheEye

6

pójdę z czymś takim:

// Setup what you're searching and what you want to find 
NSString *string = @"abcabcabcabc"; 
NSString *toFind = @"abc"; 

// Initialise the searching range to the whole string 
NSRange searchRange = NSMakeRange(0, [string length]); 
do { 
    // Search for next occurrence 
    NSRange range = [string rangeOfString:toFind options:0 range:searchRange]; 
    if (range.location != NSNotFound) { 
     // If found, range contains the range of the current iteration 

     // NOW DO SOMETHING WITH THE STRING/RANGE 

     // Reset search range for next attempt to start after the current found range 
     searchRange.location = range.location + range.length; 
     searchRange.length = [string length] - searchRange.location; 
    } else { 
     // If we didn't find it, we have no more occurrences 
     break; 
    } 
} while (1); 
+0

To droga do komplikacji, jest w tym celu: '- (void) enumerateSubstringsInRange: (NSRange) opcje zasięgu: (NSStringEnumerationOptions) używa blokadyBlock: (void (^) (podciąg NSString *, podłańcuch NSRange, zmiana w NSRange, blok BOOL *)) block'. – calimarkus

+0

To jest bardzo dobry punkt. +1 na twoją odpowiedź na to. – mattjgalloway

+0

Bardzo pomocna po wielu poszukiwaniach. Dzięki. +1 –

1

Czasami warto spojrzeć na NSString class Documentation.

Znalezienie Znaki i Podciągi

– rangeOfCharacterFromSet: 
– rangeOfCharacterFromSet:options: 
– rangeOfCharacterFromSet:options:range: 
– rangeOfString: 
– rangeOfString:options: 
– rangeOfString:options:range: 
– rangeOfString:options:range:locale: 
– enumerateLinesUsingBlock: 
– enumerateSubstringsInRange:options:usingBlock: 

Dzielenie ciągi

– componentsSeparatedByString: 
– componentsSeparatedByCharactersInSet: 
– stringByTrimmingCharactersInSet: 
– substringFromIndex: 
– substringWithRange: 
– substringToIndex: 
5

Jeśli chcesz dokonać zmian, można użyć:

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

ale jeśli to nie pasuje do Twoich potrzeb spróbuj tego:

- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block

+0

Zobacz dokumentację, jak zauważyła HelmiB. – calimarkus

0

Rozszerzając @ TheEye na answer, I gotowane to up:

@interface NSString (EnumerateOccurancesOfString) 

- (void)enumerateOccurancesOfString:(NSString *)string usingBlock:(void (^)(NSRange substringRange, BOOL *stop))block; 

@end 

-

@implementation NSString (EnumerateOccurancesOfString) 

- (void)enumerateOccurancesOfString:(NSString *)string usingBlock:(void (^)(NSRange range, BOOL * _Nonnull stop))block { 

    NSParameterAssert(block); 

    NSRange range = [self localizedStandardRangeOfString:string]; 

    if (range.location == NSNotFound) return; 


    // Iterate all occurances of 'string' 
    while (range.location != NSNotFound) 
    { 
     BOOL stop = NO; 

     block(range, &stop); 

     if (stop) { 
      break; 
     } 

     // Continue the iteration 
     NSRange nextRange = NSMakeRange(range.location + 1, self.length - range.location - 1); 
     range = [self rangeOfString:string options:(NSStringCompareOptions)0 range:nextRange locale:[NSLocale currentLocale]]; // Will this sometimes conflict with the initial range obtained with -localizedStandardRangeOfString:? 
    } 
} 

@end 
Powiązane problemy