3

I have this NSString:

NSString *test = @"three is a number";

I want to check if this NSString begin with "three"

4 Answers 4

15

Try using the hasPrefix method:

if([test hasPrefix:@"three"])
{
  // Begins with three
}
Sign up to request clarification or add additional context in comments.

Comments

4

For a more general approach, get a relevant substring, and compare it with the desired result.

if ([[test substringWithRange:NSMakeRange(0,5)] caseInsensitiveCompare:@"three"] == NSOrderedSame) {
     // ....
}

Comments

3

It's done by

if([test hasPrefix:@"three"]){
   ...
}

General answer: whenever you want to check if NSString (or any Apple-provided class,) look up the official documentation! In this case, see here. You see, there's not only StackOverflow on the internet, there's the official documentation!

Comments

2

You need to use the following:

if ([test hasPrefix:@"three"])
{
  // if YES
}

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.