Strings in Objective-C are denoted by NSString. Its subclass NSMutableString offers multiple ways to create objects regarding strings.
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;}
NSAutoreleasePool interface 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.
John.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. |
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;}
NSAutoreleasePool interface reference is created. uppercaseString to capitalize the string s2 to s3.stringByAppendingFormat to concatenate strings s1 and s2.s3 length to length variable.initWithFormat to show s1 and s2 in the specified format "%@ %@". We first print string s1, then put a space character, and finally print s2.