-1

my code

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

this code only show 1 "hello" but i need the app show me 3 "hello"

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

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?

Community
  • 1
  • 1
Zia
  • 14,622
  • 7
  • 40
  • 59