Comment voir si une NSSsortingng commence par une autre chaîne?

J’essaie de vérifier si une chaîne que je vais utiliser comme URL commence par http. La façon dont j’essaie de vérifier maintenant ne semble pas fonctionner. Voici mon code:

NSMutableSsortingng *temp = [[NSMutableSsortingng alloc] initWithSsortingng:@"http://"]; if ([businessWebsite rangeOfSsortingng:@"http"].location == NSNotFound){ NSSsortingng *temp2 = [[NSSsortingng alloc] init]; temp2 = businessWebsite; [temp appendSsortingng:temp2]; businessWebsite = temp2; NSLog(@"Updated BusinessWebsite is: %@", businessWebsite); } [web setBusinessWebsiteUrl:businessWebsite]; 

Des idées?

Essayez ceci: if ([mySsortingng hasPrefix:@"http"]) .

Au fait, votre test devrait être != NSNotFound au lieu de == NSNotFound . Mais disons que votre URL est ftp://my_http_host.com/thing , elle correspondra mais ne devrait pas.

J’aime utiliser cette méthode:

 if ([[temp subssortingngToIndex:4] isEqualToSsortingng:@"http"]) { //starts with http } 

ou même plus facile:

 if ([temp hasPrefix:@"http"]) { //do your stuff } 

Si vous recherchez “http:”, vous souhaiterez probablement une recherche insensible à la casse:

 NSRange prefixRange = [temp rangeOfSsortingng:@"http" options:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; if (prefixRange.location == NSNotFound) 

Version rapide:

 if line.hasPrefix("#") { // checks to see if a ssortingng (line) begins with the character "#" } 

C’est ma solution au problème. Cela enlèvera les lettres qui ne sont pas nécessaires et qui sont insensibles à la casse.

  - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self generateSectionTitles]; } -(NSArray *)generateSectionTitles { NSArray *alphaArray = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; NSMutableArray *sectionArray = [[NSMutableArray alloc] init]; for (NSSsortingng *character in alphaArray) { if ([self ssortingngPrefix:character isInArray:self.depNameRows]) { [sectionArray addObject:character]; } } return sectionArray; } -(BOOL)ssortingngPrefix:(NSSsortingng *)prefix isInArray:(NSArray *)array { for (NSSsortingng *str in array) { //I needed a case insensitive search so [str hasPrefix:prefix]; would not have worked for me. NSRange prefixRange = [str rangeOfSsortingng:prefix options:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; if (prefixRange.location != NSNotFound) { return TRUE; } } return FALSE; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSSsortingng *)title atIndex:(NSInteger)index { NSInteger newRow = [self indexForFirstChar:title inArray:self.depNameRows]; NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRow inSection:0]; [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; return index; } // Return the index for the location of the first item in an array that begins with a certain character - (NSInteger)indexForFirstChar:(NSSsortingng *)character inArray:(NSArray *)array { NSUInteger count = 0; for (NSSsortingng *str in array) { //I needed a case insensitive search so [str hasPrefix:prefix]; would not have worked for me. NSRange prefixRange = [str rangeOfSsortingng:character options:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; if (prefixRange.location != NSNotFound) { return count; } count++; } return 0; }