如何通过与xcode中的字符串值相比较,从数组中获取单个元素?

时间:2021-08-12 04:14:11

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.

如何通过与字符串值的比较从数组中获取单个元素。文本框中有一个字符串。我想将textfield字符串与数组进行比较。我想让这个元素形成这个数组。

4 个解决方案

#1


7  

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的NSArray你只想知道文本字段字符串是否在你可以使用的数组中:

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.

如果index == NSNotFound数组不包含文本字段字符串。

#2


0  

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.

我认为这对你有帮助。

#3


0  

Use isEqualToString: method for campare two String.

使用isEqualToString: campare两个字符串的方法。

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

#4


0  

use this :

用这个:

for (NSString * string in yourArray) {
    if ([string isEqualToString:textField.text])
    {       
      NSLog(@" They are equal");        
    }
    else
    {
      NSLog(@" They are not");
    }
}

#1


7  

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的NSArray你只想知道文本字段字符串是否在你可以使用的数组中:

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.

如果index == NSNotFound数组不包含文本字段字符串。

#2


0  

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.

我认为这对你有帮助。

#3


0  

Use isEqualToString: method for campare two String.

使用isEqualToString: campare两个字符串的方法。

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

#4


0  

use this :

用这个:

for (NSString * string in yourArray) {
    if ([string isEqualToString:textField.text])
    {       
      NSLog(@" They are equal");        
    }
    else
    {
      NSLog(@" They are not");
    }
}