Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
September 20, 2018, 12:29:05 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Testing the hasPrefix and hasSuffix methods (Am I posting in the correct forum?)  (Read 3338 times)
daviddelmonte
Full Member
***
Posts: 123



WWW Email
« on: January 12, 2010, 10:34:45 AM »

Here's two programs - one to test each method...

CODE - hasPrefix

Code: (Objective-C)
#import <Foundation/NSObject.h>  
#import <Foundation/NSString.h> 
#import <Foundation/NSAutoreleasePool.h> 

int main (int argc, const char * argv[]) 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString    *testStr = @"Testing the hasPrefix NSString method"; 

    NSString    *searchStr1 = @"Test"; 
    NSString    *searchStr2 = @"meth"; 
     

    if ( [testStr hasPrefix: searchStr1] == YES) 
        NSLog(@"'%@'\n matches prefix characters in '%@'!", searchStr1, testStr); 
else 
        NSLog(@"'%@'\n does not match the prefix characters of '%@'!", searchStr1, testStr); 

if ( [testStr hasPrefix: searchStr2] == YES) 
        NSLog(@"'%@'\n matches prefix characters in '%@'!", searchStr2, testStr); 
else 
        NSLog(@"'%@'\n does not match the prefix characters of '%@'!", searchStr2, testStr);




    [pool drain]; 
    return 0; 


CODE - hasSuffix

Code: (Objective-C)
#import <Foundation/NSObject.h>  
#import <Foundation/NSString.h> 
#import <Foundation/NSAutoreleasePool.h> 

int main (int argc, const char * argv[]) 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString    *testStr = @"Testing the hasSuffix NSString method"; 

    NSString    *searchStr1 = @"Test"; 
    NSString    *searchStr2 = @"meth"; 


    if ( [testStr hasSuffix: searchStr1] == YES) 
        NSLog(@"'%@'\n matches suffix characters in '%@'!", searchStr1, testStr); 
else 
        NSLog(@"'%@'\n does not match the suffix characters of '%@'!", searchStr1, testStr); 

if ( [testStr hasSuffix: searchStr2] == YES) 
        NSLog(@"'%@'\n matches suffix characters in '%@'!", searchStr2, testStr); 
else 
        NSLog(@"'%@'\n does not match the suffix characters of '%@'!", searchStr2, testStr);


    [pool drain]; 
    return 0; 





Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: January 12, 2010, 10:50:47 AM »

David,

Did you mean to set searchStr2 to @"method" so that you would get a match for the hasSuffix: method?

Cheers,

Steve
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email
« Reply #2 on: January 12, 2010, 11:21:50 AM »

oops. test test test..

Thanks Steve..

Code: (Objective-C)
#import <Foundation/NSObject.h>  
#import <Foundation/NSString.h> 
#import <Foundation/NSAutoreleasePool.h> 

int main (int argc, const char * argv[]) 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString    *testStr = @"Testing the hasSuffix NSString method"; 

    NSString    *searchStr1 = @"Test"; 
    NSString    *searchStr2 = @"thod"; 


    if ( [testStr hasSuffix: searchStr1] == YES) 
        NSLog(@"'%@'\n matches suffix characters in '%@'!", searchStr1, testStr); 
else 
        NSLog(@"'%@'\n does not match the suffix characters of '%@'!", searchStr1, testStr); 

if ( [testStr hasSuffix: searchStr2] == YES) 
        NSLog(@"'%@'\n matches suffix characters in '%@'!", searchStr2, testStr); 
else 
        NSLog(@"'%@'\n does not match the suffix characters of '%@'!", searchStr2, testStr);


    [pool drain]; 
    return 0; 


Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.