How to check if a string contains a substring in iOS?


 

How to check if a string contains a substring in iOS?

How to check if a string contains a substring in iOS?

How to check if a string contains a substring in iOS?

Function for checking the presence of a substring in a string (NSString) in iOS

While writing the code for iOS based application you may have to find the presence of substring in a string (NSString) and there is no readymade function in iOS. In Java programming language you can use the indexOf method of the String class but there is no such function in iOS.

In this tutorial I will explain you about a function which can be easily used to find the existence of a substring in a string. This function will simply tell you if NSString object is present in another object of NSString of not.

The NSString class is very important class in iOS programming as it is one of the most used class and it is used to manage the text data in iOS programming. The NSString class comes with the Unicode support out of the box. This class is very useful and it provides many methods for performing search and manipulating the text data.

In this example we have created a simple iOS project and then added two fields one for original text and other for entering the text to find. When user clicks on the 'Check If Exists' button application will find the string and show the appropriate message.

Here is the screen shot of example application:

How to check if a string contains a substring in iOS?

Here is the video explanation of the tutorial which teaches you how to run the example code:

In the .h file you can define the function as given below:

- (BOOL) ifContainsString: (NSString*) substring source:(NSString *)sourceString;

The implementation code is:

- (BOOL) ifContainsString: (NSString*) substring source:(NSString *)sourceString{
    return [sourceString rangeOfString:substring].location != NSNotFound;
}

The full code of the header file is:

//
//  ViewController.h
//  TestStringContrains
//
//  Created by Deepak Kumar on 25 March 2015.
//  Copyright (c) Rose India Technologies Pvt. Ltd. Deepak Kumar. All rights reserved.
//

#import 

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *txtSourceString;

@property (weak, nonatomic) IBOutlet UITextField *txtStringToFind;


	- (BOOL) ifContainsString: (NSString*) substring source:(NSString *)sourceString;


- (IBAction)compareString:(id)sender;

@end

And the full code of .m file is:

//
//  ViewController.m
//  TestStringContrains
//
//  Created by Deepak Kumar on 25 March 2015.
//  Copyright (c) Rose India Technologies Pvt. Ltd. Deepak Kumar. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize txtSourceString;
@synthesize txtStringToFind;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (BOOL) ifContainsString: (NSString*) substring source:(NSString *)sourceString{
    return [sourceString rangeOfString:substring].location != NSNotFound;
}

- (IBAction)compareString:(id)sender {
    BOOL result = [self ifContainsString:txtStringToFind.text source:txtSourceString.text];
    if(result){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Compare String"
                                                        message:@"String found!!"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Compare String"
                                                        message:@"String not found!!"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

The code:

[sourceString rangeOfString:substring].location

is used to find if a string present in the source string.

In this tutorial you have learned how to find it a string contains in the source string.

Download the Source code of the Project.

Ads