Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the text and perform various operations. A string is a sequence of characters, such as letters, digits, and symbols. In Objective-C, a string is an object that represents this sequence of characters. A string can be created using a class, which is a blueprint for creating objects. Once you have created a string object, you can use various methods to manipulate the text and perform operations on it. Objective-C provides several types of strings that can be used to create and manipulate text.
Types of Strings
- NSString: It is an immutable object that represents a sequence of characters. Once created, it cannot be changed. NSString is the most commonly used type of string in Objective-C.
- NSMutableString: It is a mutable object that represents a sequence of characters. It can be modified after creation, and new characters can be added or removed.
- CFString: It is a Core Foundation object that represents a sequence of Unicode characters.
Built-in Methods for Strings
Objective-C provides several built-in classes and methods for creating and manipulating strings. Some of the most commonly used methods are:
Methods |
Purpose |
---|---|
NSString | It is an immutable class used to create a string that cannot be changed once created. |
NSMutableString | It is a mutable class used to create a string that can be changed after it is created. |
length | It is a method used to get the length of the string. |
stringWithFormat | It is used to format a string with values. |
substringWithRange | It is a method used to get a substring of the string. |
isEqualToString | It is a method used to compare two strings for equality. |
stringByAppendingFormat | It is a method used to append a formatted string to the end of a string. |
Example 1:
In the following example, we are going to create a string using NSString.
ObjectiveC
#import <Foundation/Foundation.h> int main ( int argc, const char * argv[]){ // define string NSString *myString = @"Hello, World!" ; // print result NSLog ( @"%@" , myString); return 0; } |
When the preceding code is compiled and run, the following output is obtained:
Example 2:
In the following example, we are going to format a string with values using stringWithFormat.
ObjectiveC
#import <Foundation/Foundation.h> int main ( int argc, const char * argv[]){ // create a int variable int count = 10; // format a string with a value using stringWithFormat NSString *myFormattedString = [ NSString stringWithFormat: @"I have %d apples." , count]; // print result NSLog ( @"%@" , myFormattedString); return 0; } |
When the preceding code is compiled and run, the following output is obtained:
Example 3:
In the following example, we create and manipulate strings in Objective-C:
ObjectiveC
#import <Foundation/Foundation.h> int main ( int argc, const char * argv[]){ // Create a string using the NSString class NSString *myString = @"Hello, World!" ; // Get the length of the string NSUInteger length = [myString length]; // Get a substring of the string NSString *substring = [myString substringWithRange: NSMakeRange (7, 5)]; // Compare the string to another string BOOL isEqual = [myString isEqualToString: @"Hello, World!" ]; // Create a mutable string using the NSMutableString class NSMutableString *mutableString = [ NSMutableString stringWithString: @"Hello" ]; // Append a string to the end of the mutable string [mutableString appendString: @" World!" ]; // Add two strings together NSString *string1 = @"Hello" ; NSString *string2 = @"World" ; NSString *result = [string1 stringByAppendingFormat: @" %@" , string2]; // Print the results to the console NSLog ( @"Original string: %@" , myString); NSLog ( @"Length of string: %lu" , length); NSLog ( @"Substring of string: %@" , substring); NSLog ( @"String is equal: %d" , isEqual); NSLog ( @"Mutable string: %@" , mutableString); NSLog ( @"Concatenated string: %@" , result); return 0; } |
When the preceding code is compiled and run, the following output is obtained:
Contact Us