2010-08-03 7 views
4

Comment puis-je savoir si une chaîne contient quelque chose? Quelque chose comme:iPhone Objective-C: Si la chaîne contient ...?

if([someTextField.text containsString:@"hello"]) { 

} 
+0

il n'y a rien de mal à moins ici :) – vodkhang

+0

Identique [ chaîne contient la chaîne en Objective-c (iphone) ] (http://stackoverflow.com/questions/2753956/string-contains- string-in-objectif-c-iphone). –

Répondre

22

Vous pouvez utiliser:

if (result && [result rangeOfString:@"hello"].location != NSNotFound) { 
    // Substring found... 
} 
7

Vous devez utiliser - (NSRange)rangeOfString:(NSString *)aString

NSRange range = [myStr rangeOfString:@"hello"]; 
if (range.location != NSNotFound) { 
    NSLog (@"Substring found at: %d", range.location); 
} 

plus ici: NSString rangeOfString

+1

Dans quel Univers est cette réponse "un peu difficile"? – JeremyP

+0

Parce que pour moi, de l'arrière-plan de Java. Nous devrions utiliser [str contains:], donc quand j'ai découvert l'API, ça a l'air un peu étrange. Je pense que le questionneur pense la même chose donc je dis un peu difficile – vodkhang

+0

hahahaha Jeremy's post vient de me faire lol – Baconbeastnz

2

Si l'intention de votre code est vérifier si une chaîne contient une autre chaîne, vous pouvez créer une catégorie pour clarifier cette intention . Je n'ai pas compilé ce code, donc peut-être que vous devriez le changer un peu.

Quentin

Questions connexes