Objective-C functions similar to Regular Expression

April 16, 2009 § Leave a comment

You can easily find matching pattern string using Objective-C methods hasPrefix: and hasSuffix:.

You can also find the substring within a string by using NSRange object and rangeOfString: method.

NSString *filename = @”iPhone_developer_book.html”;
if ([filename hasPrefix: @”iPhone”) {
// this is iphone content
}

if ([filename hasSuffix:@”.html”) {
// this is a html
}

NSRange range;
range = [filename rangeOfString: @”developer”];

This range statement returns with range.start at 7 and range.length set to 9 because string “developer” starts at position 7 and the length of string is 9.

If it couldn’t find the rangeOfString with string “developer”, range.start will be equal to NSNotFound.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Objective-C functions similar to Regular Expression at Web Builders.

meta

%d bloggers like this: