iPhone String Match


 

iPhone String Match

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.

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


Ads