Reputation:
how to get a single element from an array by comparing with a string value.I have a string in a textfield.I want to compare that textfield string with an array.And i want to get that single element form that array.
Upvotes: 3
Views: 2539
Reputation: 974
use this :
for (NSString * string in yourArray) {
if ([string isEqualToString:textField.text])
{
NSLog(@" They are equal");
}
else
{
NSLog(@" They are not");
}
}
Upvotes: 0
Reputation: 862
Use isEqualToString: method for campare two String.
for (int i=0; i<[array count]; i++) {
NSString * string = [array objectAtIndex:i];
i if (string isEqualToString:textField.text)
{
NSLog(@"Equal");
}
else
{
NSLog(@"Not Equal");
}
}
Upvotes: 0
Reputation: 6718
Use compare:
method.
for (int i=0; i<[yourArray count]; i++) {
NSString * string = [yourArray objectAtIndex:i];
if ([textfield.text compare:string]) {
NSLog(@"yes");
break;
}
}
I think it will be helpful to you.
Upvotes: 0
Reputation: 5589
If you have an NSArray
of NSString
's and you just want to see whether or not the text field string is in the array you can use:
NSString *textFieldString; // Contents of my text field
NSArray *myArray; // Array to search
BOOL stringMatches = [myArray containsObject:textFieldString];
If you instead want to know the index of the string in the array use:
NSUInteger index = [myArray indexOfObject:textFieldString];
If index == NSNotFound
the array does not contain the text field string.
Upvotes: 7