Tuesday, September 7, 2010

NSString and NSMutableString

NSString: NSString is a class which handles immutable strings (immutable strings are those strings which cannot be changed). 

The mutable subclass is NSMutableString.

NSString declares methods for finding and comparing strings. It also declares methods for reading numeric values from strings, for combining strings in various ways, and for converting a string in different forms (case change such as upper case, lower case etc)

To use Strings in your application you have to import the following header file


NSString.h


You’ve encountered string objects in your programs before.Whenever you enclosed a sequence of character strings inside a pair of double quotes,as in

@"Programming is fun"


you created a character string object in Objective-C.

Now lets have a look at some of the string functions that are more often used while making an application such as:

  1. copy strings.
  2. Append String.
  3. Equality check.
  4. Which string is greater.
  5. change case.
Given below is the sample code with explanation 

//declare a class 


@interface Myclass : NSObject

{

}

-(void)copystring;           //copy one string to another

-(void)copy_string_at_end;  //append string

-(void)Equality_Check;     //check whether string is equal or not

-(void)great_check;       //check which string is greater

-(void)case_check;      //convert from upper case to lower case and vice versa


@end


//implement the class


@implementation Myclass


-(void)copystring

{

NSString *str1 =@"This is string R1"; //str1 is string object

NSString *str2 = @"This is string R2"; //str2 is string object

//copy str1 in str2 and display result

str2 = [NSString stringWithString:str1];

NSLog(@"%@",str2);  //print str2 on console

}


-(void)copy_string_at_end

{

NSString *str1 =@"This is string R1"; //str1 is string object

NSString *str2 = @"This is string R2"; //str2 is string object

//copy str2 at the end of str1

str2 = [str1 stringByAppendingString:str2];

NSLog(@"%@",str2);  //print str2 on console

}


-(void)Equality_Check

{

NSString *str1 = @"Radix";

NSString *str2 = @"RadiX";

//checking whether strings are eaual or not

if([str1 isEqualToString:str2])

{

NSLog(@"str1 = str2"); 

}

else

{

NSLog(@"str1 != str2");

}

}


-(void)great_check

{

//here the strings are lexically compared means their ASCII values are compared

NSComparisonResult compareresult;

NSString *str1 = @"Radix";

NSString *str2 = @"RadiX";

//for case sensitive compare

compareresult = [str1 compare:str2];

//if you don't want to do perform case sensitive comparision then use the following code

//compareresult = [str1 caseInsensitiveCompare:str2];

if(compareresult == NSOrderedAscending)

{

NSLog(@"str1 is less than str2");

}

else if(compareresult == NSOrderedSame)

{

NSLog(@"str1 == str2");

}

else

{

NSLog(@"str1 > str2");

}

}


-(void)case_check

{

NSString *str1= @"this string will be in uppercase";

NSString *str2 = @"THIS STRING WILL BE IN LOWER CASE";

str1 = [str1 uppercaseString];  //convert lowercase to uppercase.

str2 = [str2 lowercaseString];  //convert uppercase to lowercase.

NSLog(@"%@",str1);

NSLog(@"%@",str2);

}


@end


Now into the main method create the object of your class and then call the methods


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

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


    // insert code here...

    Myclass *obj = [[Myclass alloc]init];

[pool addObject:obj]; //adding object to pool

[obj copystring];

[obj copy_string_at_end];

[obj Equality_Check];

[obj great_check];

[obj case_check];

    [pool drain];

    return 0;

}


The output of the above code will look something like this




If you want to know the length of a particular string then you have an object method called as length.

NSLog(@"The length of the string is %d",[str1 length]);


The above code will give you the length of the string str1.


Now let's have a neat workout with substrings section


inorder to begin i have just add a function named :


-(void)Substring_function;


-(void)Substring_function

{

NSString *str1= @"This is string R1";

//The below line of code print the string upto the specified index number starting from the leading character.

NSLog(@"String at index 3 of str1 is : %@",[str1 substringToIndex:3]);

//if you want to print the substring from the specified index then use the below function

NSLog(@"Substring from specified index is : %@",[str1 substringFromIndex:5]);

//now lets say you have a long string and you want string only from a selected index then you can use the below function.

NSLog(@"Substring from a specified index to a specified index is : %@",[[str1 substringFromIndex:3]substringToIndex:6]);

//now lets say you want to locate a particular string inside your string then in that case you can use the below code

NSRange Myrange;  

//NSRange is a structure which is used to describe a range of series such as charcters in a string and objects in an array


Myrange = [str1 rangeOfString:@"R1"];

NSLog(@"The string is found at location : %d and is of length : %d",Myrange.location,Myrange.length);

//but what if the string is not found then what to do

Myrange = [str1 rangeOfString:@"R2"];

if(Myrange.location == NSNotFound)

{

NSLog(@"No such string found");

}

else

{

NSLog(@"The string is found at location : %d and is of length : %d",Myrange.location,Myrange.length);

}

}


The output of the following function will look something like this in the image given below:




In my next blog i will discuss MutableString

3 comments:

  1. Hey nice one. Post NSString Encoding & other facte related to NSString so that other s will get this clearly.
    Mahesh

    ReplyDelete
  2. plz post related NSString Encoding

    ReplyDelete