Objective C Tutorial - Objective C String Operations






Append sub string

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    [largeString appendString: @" and this is a string too."];
    NSLog (@"New string = %@", largeString);  
 
   return 0;
}

The code above generates the following result.





Check prefix and suffixes

#import <Foundation/Foundation.h>
 
int main ()
{
  NSString *myName = @"John Doe";
    NSString *hisName = @"John Doe";
    if ([myName isEqualToString: hisName])
    {
        NSLog (@"The two strings are equal using isEqualToString.");
    }
    BOOL flag;
    flag = [myName hasPrefix: @"John"];
    if (flag)
    {
        NSLog (@"The hasPrefix method returned YES.");
    }  
 
   return 0;
}

The code above generates the following result.





Convert string to number

#import <Foundation/Foundation.h>
 
int main ()
{
  NSString *integerString = @"47";
    NSString *floatString = @"52.7016";
    int myInteger;
    float myFloat;
    myInteger = [integerString intValue];
    myFloat = [floatString floatValue];
    NSLog (@"Integer value = %i", myInteger);
    NSLog (@"Float value = %f", myFloat);
    NSLog (@"**********");
    myInteger = [floatString integerValue];
    myFloat = [integerString floatValue];
    NSLog (@"Integer value = %i", myInteger);
    NSLog (@"Float value = %f", myFloat);  
 
   return 0;
}

The code above generates the following result.

Convert to uppercase and lowercase

#import <Foundation/Foundation.h>
 
int main ()
{
  NSString *testString = @"Greetings from another planet!";
    NSString *targetString;
    targetString = [testString uppercaseString];
    NSLog (@"All uppercase = %@", targetString);
    NSLog (@"**********");
    targetString = [testString lowercaseString];
    NSLog (@"All lowercase = %@", targetString);
    NSLog (@"**********");
    targetString = [testString capitalizedString];
    NSLog (@"All capitalized strings = %@", targetString);
    NSLog (@"**********");
    NSLog (@"Original string = %@", testString);  
 
   return 0;
}

The code above generates the following result.

Delete part of a string


#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    [largeString deleteCharactersInRange: [largeString rangeOfString: @"is a "]];
    NSLog (@"New string = %@", largeString);  
 
   return 0;
}

The code above generates the following result.

Extracting a Substring to the End of a String

#import <Foundation/Foundation.h>
 
int main ()
{

  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    NSString *newString;
    newString = [largeString substringFromIndex: 5];
    NSLog (@"New string = %@", newString);  
 
   return 0;
}

The code above generates the following result.

Extracting a Substring with Location and Length

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    NSString *newString;
    newString = [largeString substringWithRange: NSMakeRange(5, 4)];
    NSLog (@"New string = %@", newString);  
 
   return 0;
}

The code above generates the following result.

Get the length of a string

#import <Foundation/Foundation.h>
 
int main ()
{
  NSString *myString = @"Hello, world!";
    int counter;
    counter = [myString length];
    NSLog (@"String length = %i", counter);  
 
   return 0;
}

The code above generates the following result.

Insert a string

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    [largeString insertString: @"was and still " atIndex: 5];
    NSLog (@"New string = %@", largeString);  
 
   return 0;
}

The code above generates the following result.

Searching and Replacing Strings

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    [largeString replaceCharactersInRange: NSMakeRange(5,2) withString: @"was not"];
    NSLog (@"New string = %@", largeString);
 
   return 0;
}

The code above generates the following result.

Searching and Replacing Part of a String

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableString *largeString;
    largeString = [NSMutableString stringWithString: @"That is a string"];
    NSLog (@"Original string = %@", largeString);
    [largeString replaceCharactersInRange: [largeString rangeOfString: @"is"]  withString: @"was not"];
    NSLog (@"New string = %@", largeString);
 
   return 0;
}

The code above generates the following result.

Searching for a Substring

#import <Foundation/Foundation.h>
 
int main ()
{
  NSRange myRange;
    NSString *bigString = @"Learning to program can be fun!";
    myRange = [bigString rangeOfString: @"can be"];
    if (myRange.location == NSNotFound)
    {
        NSLog (@"Substring is not in %@", bigString);
    }
    else
    {
        NSLog (@"Substring found at location = %i", myRange.location);
        NSLog (@"Substring length = %i", myRange.length);
    }
 
   return 0;
}

The code above generates the following result.

Compare two Strings

#import <Foundation/Foundation.h>

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

        
        NSString *myString1 = @"A";
        NSString *myString2 = @"B";
        NSString *myString3 = @"A";
        
        BOOL isEqual = [myString1 isEqualToString:myString2];
        
        if(isEqual)
            NSLog(@"%@ is equal to %@", myString1, myString2);
        else
            NSLog(@"%@ is not equal to %@", myString1, myString2);
            
        if([myString1 isEqualToString:myString2])
            NSLog(@"%@ is equal to %@", myString1, myString2);
        else
            NSLog(@"%@ is not equal to %@", myString1, myString2);
        
        if([myString1 isEqualToString:myString3])
            NSLog(@"%@ is equal to %@", myString1, myString3);
        else
            NSLog(@"%@ is not equal to %@", myString1, myString3);
        
        NSString *name = @"Mr. John Smith, MD";
        
        BOOL hasMrPrefix = [name hasPrefix:@"Mr"];
        
        if(hasMrPrefix)
            NSLog(@"%@ has the Mr prefix", name);
        else
            NSLog(@"%@ doesn't have the Mr prefix", name);
        
        BOOL hasMDSuffix = [name hasSuffix:@"MD"];
        
        if(hasMDSuffix)
            NSLog(@"%@ has the MD suffix", name);
        else
            NSLog(@"%@ doesn't have the MD suffix", name);
        
        NSString *alphabet = @"ABCDEFGHIJKLMONPQRSTUVWXYZ";
        
        NSRange range = NSMakeRange(2, 3);
        
        BOOL lettersInRange = [[alphabet substringWithRange:range] isEqualToString:@"CDE"];
        
        if(lettersInRange)
            NSLog(@"The letters CDE are in alphabet starting at position 2");
        else
            NSLog(@"The letters CDE aren't in alphabet starting at position 2");
    return 0;
}

The code above generates the following result.

Get a Range of String

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSMutableString *myString = [[NSMutableString alloc] initWithCapacity:26];

        [myString setString:@"ABCDEFGHIJKLMONPQRSTUVWXYZ"];
        
        NSLog(@"%@", myString);
        
        [myString appendString:@", 0123456789"];
        
        NSLog(@"%@", myString);
        
        [myString insertString:@"abcdefg, "
                       atIndex:0];
        
        NSLog(@"%@", myString);
        
        NSRange range = NSMakeRange(9, 3);
        
        [myString deleteCharactersInRange:range];
        
        NSLog(@"%@", myString);
        NSRange rangeOfString = [myString rangeOfString:myString];
        
        [myString replaceOccurrencesOfString:@", "
                                  withString:@"|"
                                     options:NSCaseInsensitiveSearch
                                       range:rangeOfString];
        
        NSLog(@"%@", myString);
        

        NSRange rangeToReplace = NSMakeRange(0, 4);
        
        [myString replaceCharactersInRange:rangeToReplace
                                withString:@"MORE"];
        
        NSLog(@"%@", myString);

    
    return 0;
}

The code above generates the following result.