Objective C Strings

05 Sep

In this post we shall have a look at the NSString class in objective c which is used for handling strings.

NSString class: The NSString class is used to handle immutable strings, the mutable class of NSString is NSMutableString. With the help of the NSString class you can find and compare particular strings, combine strings and convert the string into a different forms like change their case or some encoding changes. Let’s have a look at some of the examples which are given below which will help you to give a basic idea on how to use NSStirng class.

Declaring a string: In Objective c the syntax to declare a string is given below:

NSString *str = @”This is how you declare string variable”;

In the above example you can see how we declare a string variable in objective c, you can also declare a string variable like the one give below

NSString *str = [[NSString alloc]init];
str = @”This is another method to declare a string”;

Now the question in your mind would be which method to use to declare a string well the answer to this is very simple any one of the above would do but make very sure that if you are using the second approach then in that case be very sure to release the memory occupied by that string as a part of memory management (we shall discuss this later).

Determining String length: If you want to find out what’s the length of a particular string then in that case you may use the inbuilt instance method of NSString class called as length here’s an image displaying  on how to use the length function

 Code Explanation: In the above the function named getStringLength is making the use of the string function called length to return the string length.

Concatenating Strings:  Their might be scenarios where you want to append one string with another so here’s a function of NSString class to do that concatenated

Code Explanation: In the above code you can see that there are two different strings str1 and str2 each carrying different data, now if you want to append the data of str2 with str1 then in that case use the instance method of the NSString class called as the stringByAppendingString now if you run the above code it will give the output as below


Creating Format Strings: Using NSString class static method called as stringWithFormat you can create your own formatted string.

Code Explanation:
In the above code I have used the static function called as stringWithFormat which will return a formatted string which can be used later in the code, the function stringWithFormat is similar to the java static function called format. Given below is the output of the above function

Converting String to number: Frequently a program ends up with numeric data into string object so if you want to use that numeric data it becomes impossible to use it as it is in the string format so in such scenarios a conversion needs to be done from string to number the example for which is given below

Code Explanation: if you want to convert a string into a integer, float or double number then in that case you may use the instance method of the NSString class to do that which will help you in converting the string. Given below is the output image

 Searching a String: There are times when you would like to determine whether a particular string is present or not and if it is present then at what location so for that you may use the NSRange structure with strings

 Code Explanation: str is a string variable and in case if we want to find a particular data present inside the string then in that case you have to use the structure called NSRange and using the rangeOfString method what you could do is specify the string that you are searching for.


String Comparison: The NSString class has a method called isEqualToString which is used to compare one string with other.

 I hope that this post has helped you out in learning strings in objective c you can find the PDF copy of this post here, kindly let me know if you have any queries regarding strings via comments or you can mail me at until then Happy iCoding and have a great day.


Posted by on September 5, 2011 in Beginners


Tags: , ,

2 responses to “Objective C Strings

  1. Milind Mahajan

    September 6, 2011 at 11:20 am

    Bahot Khub sirjee..! Achha explanation diya hai basic concepts ka.. 🙂

  2. Suraj R. Mirajkar

    September 12, 2011 at 8:35 am

    NSString *string1 = @”Hello”;
    NSString *string2 = @”hello”;
    if([string1 caseInsensitiveCompare:string2]==NSOrderedSame)
    NSLog(@”Strings are equal”);
    NSLog(@”Strings are not equal”);

    Output of above code : Strings are equal

    It checks whether the string1 is equal to the string2 without case sensitive comparison.
    NSOrderedSame checks whether the two operands are equal.

    This type of comparison is required for comparing Urername in a Login form where caseinsensitive comparison is needed.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: