1281 votes

Comment puis-je vérifier si une chaîne contient une autre chaîne en Objective-C?

Comment puis-je vérifier si une chaîne de caractères (NSString) contient une autre chaîne de plus petite?

Je m'attendais à quelque chose comme:

NSString *string = @"hello bla bla";
NSLog(@"%d",[string containsSubstring:@"hello"]);

Mais le plus proche que j'ai pu trouver était:

if ([string rangeOfString:@"hello"] == 0) {
    NSLog(@"sub string doesnt exist");
} 
else {
    NSLog(@"exists");
}

De toute façon, c'est que la meilleure façon de savoir si une chaîne contient une autre chaîne?

2490voto

Dave DeLong Points 156978
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
  NSLog(@"string does not contain bla");
} else {
  NSLog(@"string contains bla!");
}

La clé est de remarquer qu' rangeOfString: renvoie un NSRange struct, et la documentation dit qu'il renvoie la struct {NSNotFound, 0} si la "botte de foin" ne contient pas le "aiguille".


Et si vous êtes sur iOS 8 ou OS X Yosemite, vous pouvez le faire maintenant:

NSString *string = @"hello bla blah";
if ([string containsString:@"bla"]) {
  NSLog(@"string contains bla!");
} else {
  NSLog(@"string does not contain bla");
}

168voto

P i Points 6466

Faire une catégorie sur NSString:

@interface NSString ( containsCategory )
- (BOOL) containsString: (NSString*) substring;
@end

// - - - - 

@implementation NSString ( containsCategory )

- (BOOL) containsString: (NSString*) substring
{    
    NSRange range = [self rangeOfString : substring];
    BOOL found = ( range.location != NSNotFound );
    return found;
}

@end

60voto

Lukas Points 1495

Depuis ce qui semble être un haut rang de résultats dans Google, je tiens à ajouter ceci:

iOS 8 et OS X 10.10 ajouter l' containsString: méthode de NSString. Une version mise à jour de Dave DeLong l'exemple de ces systèmes:

NSString *string = @"hello bla bla";
if ([string containsString:@"bla"]) {
    NSLog(@"string contains bla!");
} else {
    NSLog(@"string does not contain bla");
}

41voto

AJS Points 677
NSString *myString = @"hello bla bla";
NSRange rangeValue = [myString rangeOfString:@"hello" options:NSCaseInsensitiveSearch];

if (rangeValue.length > 0){

NSLog(@"string contains hello");

} 

else {

NSLog(@"string does not contain hello!");

}

//Vous pouvez aussi l'utiliser à la suite de trop :

if (rangeValue.location == NSNotFound) {

NSLog(@"string does not contain hello");

} 

else {

NSLog(@"string contains hello!");

}

24voto

Govind P N Points 1016

Avec iOS 8 et rapide, nous pouvons utiliser la méthode localizedCaseInsensitiveContainsstring

 let string: NSString = "Café"
 let substring: NSString = "É"

 string.localizedCaseInsensitiveContainsString(substring) // true

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X