2

I have a NSArray structure likes this {w123,123,234,345,w124,234,235,w125,234,453,435,....}

What I am trying to do is to specify which one begins with "w" then do something But I don't know how to do that in objective C, anyone can help? Thanks in advance.

4 Answers 4

5
for (NSString *text in myArray) {
    if ([text hasPrefix:@"w"]) {
       // Do something.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can use NSPredicate.

NSString *searchStr = @"w";  // or something else
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@", searchStr];
NSArray *resultArray = [mainArray fileteredArrayUsingPredicate:predicate];

The resultArray will contain all the strings that start with searchStr(@"w", in this case).

Case sensitivity: Here c and d are used for case and diacritic insensitivity.

String comparisons are by default case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively.

Comments

1

Code as,

for(NSString * str in array)
{
 if([str hasPrefix:@"w"])
   //Do something
}

1 Comment

Dont use characterAtIndex method, if there is a string with length 0, the app will crash. So im editing my answer
0

I guess the best option would be to iterate through the array and check if that string begins with "w".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.