2014-05-05 6 views
-1

mon codeComment rechercher plusieurs mots dans un texte en Objective-C?

NSString * mistring = @"hello hello hello."; 
NSRange detect; detect = [mistring rangeOfString: @"@hello"]; 
if (detect.location == NSNotFound) 
{ NSLog(@"Not Found."); 
}else{ NSLog (@"Se ha encontrado una coincidencia en la posición: %i", detectar.location); NSLog (@"Está tiene %i caracteres.", detectar.length); } 

ce code ne montrent 1 "bonjour" mais je besoin de l'application me montrer 3 "bonjour"

Répondre

0
NSUInteger count = 0, length = [str length]; 
NSRange range = NSMakeRange(0, length); 
while(range.location != NSNotFound) 
{ 
    range = [str rangeOfString: @"cake" options:0 range:range]; 
    if(range.location != NSNotFound) 
    { 
    range = NSMakeRange(range.location + range.length, length - (range.location + range.length)); 
    count++; 
    } 
} 

Number of occurrences of a substring in an NSString?

+0

Cela ne fait aucun comptage. – rmaddy

+0

Ok, essayez-le maintenant. –

Questions connexes