What are strings in Objective-C?
Overview
Strings in Objective-C are denoted by NSString. Its subclass NSMutableString offers multiple ways to create objects regarding strings.
Syntax
NSString *Name = @"Edpresso";
Let’s discuss an example to create a string object.
#import <Foundation/Foundation.h>int main () {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];NSString *Name = @"John";NSLog(@"First Name: %@\n", Name );[pool drain];return 0;}
- Line 3: An
NSAutoreleasePoolinterface reference is created.
Note: The
NSAutoreleasePoolinterface implements an autorelease pool that stores objects that are drained before the end of program. It helps to deallocate and release memory of variables automatically without any external garbage collection.
- Line 4: We create a string with the value
John. - Line 5: We print the string to the console.
Methods for Basic String Operations
Method | Description |
| It returns the capital illustration of the string. |
| It returns the character at a specified array position. |
| It returns the decimal value of text as double. |
| It reruns the decimal value of text as the float. |
| It returns the Boolean value that shows whether the defined string matches the starting character of the receiver or not. |
| It returns the Boolean value that shows whether the defined string matches the ending character of the receiver or not. |
| It returns the NSString object. It is initialized with the help of a specified format regarding string. |
| It returns the NSInteger of the text. |
| It returns a Boolean value that shows whether the defined string is equal to the receiver or not. It is done by using a literal Unicode-based comparison. |
| It returns the number of characters regarding Unicode in the text. |
| It returns the lowercased illustration of the text. |
| It returns a new string.it contains the characters regarding receiver from the character at defined index o the last one. |
| It returns the range of the first occurrence of the defined string in the receiver. |
| It returns the string constructed by appending or joining a string made from the defined format string and the following parameters to the receiver. |
| It returns a new string. This string is constructed by removing from both ends of receiver characters confined in a present character set. |
Explanation
In the code snippet below, we’ll use built-in string operations and methods.
#import <Foundation/Foundation.h>/* String operations*/int main () {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];NSString *s1 = @"Hi,";NSString *s2 = @"John";NSString *s3;int length;/* uppercased text or string */s3 = [s2 uppercaseString];NSLog(@"Uppercase String : %@\n", s3 );/* concatenating s1 and s2 */s3 = [s1 stringByAppendingFormat:@"John"];NSLog(@"The concatenated text: %@\n", s3 );/* total length of s3 after the concatenation */length = [s3 length];NSLog(@"Length of S3 : %d\n", length );/* InitWithFormat */s3 = [[NSString alloc] initWithFormat:@ "%@ %@", s1, s2];NSLog(@"Using initWithFormat: %@\n", s3 );[pool drain];return 0;}
- Line 4: We create an
NSAutoreleasePoolinterface reference is created.
- Line 5–8: We declare three strings and one variable to keep string length.
- Line 11: We call
uppercaseStringto capitalize the strings2tos3. - Line 15: We use
stringByAppendingFormatto concatenate stringss1ands2. - Line 19: We extract string
s3length to length variable. - Line 23: We use
initWithFormatto shows1ands2in the specified format"%@ %@". We first print strings1, then put aspacecharacter, and finally prints2.