Home Tutorial Iphone Examples iPhone String Match

 
 

Share on Google+Share on Google+
iPhone String Match
Posted on: November 27, 2009 at 12:00 AM
Advertisement
iPhone String Match - In this "String match" example we are going to use "rangeofstring" method, which is used to search for a string within the given set of string.

iPhone String Match

In this "String match" example we are going to use "rangeofstring" method, which is used to search for a string within the given set of string. You can find lots more string methods that are used to search, compare and sort the strings in cocoa framework. For example... rangeofString, rangeofString:options:, rangeOfCharacterFromSet: and comparison methods.

Basically, String is a class which have the methods for finding and comparing characters or substrings within the given strings.

Let's take an example to understand it in a better way... In this example we have two different sets of string to compare from, and using "rangeofString" method we can find if one set of strings contains the set of another string in it or not.

Please find the code below..

rangeofstringAppDelegate.h

#import <UIKit/UIKit.h>

@interface rangeofstringAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    NSString *string;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSString *string;

@end

rangeofstringAppDelegate.m

#import "rangeofstringAppDelegate.h"

@implementation rangeofstringAppDelegate

@synthesize window, string;

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

    // Override point for customization after application launch
    [window makeKeyAndVisible];
   
    //NSString *compareString = @"167";
    NSCharacterSet *compareCharSet = [NSCharacterSet characterSetWithCharactersInString:@"167282"];
   
    if ([@"167282" rangeOfString:@"167"].location != NSNotFound) {
     
        NSLog(@"String contains '167'");
     
    }
    else
    {
        NSLog(@"String doesn't contain '167'.");
    }

}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

On running the application you will find the given output..

DOWNLOAD CODE


Advertisement

Related Tags for iPhone String Match :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 27, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com