Posted By

espinallab on 02/03/10


Tagged

String match


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

bobbym245
Tintenklecks
cnathan


String Match


 / Published in: Objective C
 

URL: http://www.roseindia.net/tutorial/iphone/examples/iphone-string-match.html

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.

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface rangeofstringAppDelegate : NSObject <UIApplicationDelegate> {
  4. UIWindow *window;
  5. NSString *string;
  6. }
  7.  
  8. @property (nonatomic, retain) IBOutlet UIWindow *window;
  9. @property (nonatomic, retain) NSString *string;
  10.  
  11. @end
  12.  
  13.  
  14.  
  15. #import "rangeofstringAppDelegate.h"
  16.  
  17. @implementation rangeofstringAppDelegate
  18.  
  19. @synthesize window, string;
  20.  
  21. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  22.  
  23. // Override point for customization after application launch
  24. [window makeKeyAndVisible];
  25.  
  26. //NSString *compareString = @"167";
  27. NSCharacterSet *compareCharSet = [NSCharacterSet characterSetWithCharactersInString:@"167282"];
  28.  
  29. if ([@"167282" rangeOfString:@"167"].location != NSNotFound) {
  30.  
  31. NSLog(@"String contains '167'");
  32.  
  33. }
  34. else
  35. {
  36. NSLog(@"String doesn't contain '167'.");
  37. }
  38.  
  39. }
  40.  
  41. - (void)dealloc {
  42. [window release];
  43. [super dealloc];
  44. }
  45.  
  46. @end

Report this snippet  

You need to login to post a comment.