cyclingIsBetter
cyclingIsBetter

Reputation: 17591

OBjective C: check the beginning of a string

I must verify the beginning of a string: my example string is

NSString *string1 = @"Hello World";

then I must do an if, example:

 if (string1 startwith "Hello")

How can I do this in objective c?

Upvotes: 5

Views: 7863

Answers (3)

NSResponder
NSResponder

Reputation: 16861

Read the documentation for the NSString class. You'll find all kinds of surprises.

Upvotes: 2

Mick Walker
Mick Walker

Reputation: 3847

How about doing it this way:

NSRange aRange = [myString rangeOfString:@"Hello"];
if (aRange.location ==NSNotFound) {
  NSLog(@"string not found");
} else {
  NSLog(@"string was at index %d ",aRange.location);
}

Based on the range, you can determine where in the string the item occurs

Upvotes: 1

Jim Blackler
Jim Blackler

Reputation: 23179

if ([string1 hasPrefix:@"Hello"])

Upvotes: 38

Related Questions