Strings in Objective-C
Last Updated :
27 Mar, 2023
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[]){
NSString *myString = @"Hello, World!" ;
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[]){
int count = 10;
NSString *myFormattedString = [ NSString stringWithFormat: @"I have %d apples." , count];
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[]){
NSString *myString = @"Hello, World!" ;
NSUInteger length = [myString length];
NSString *substring = [myString substringWithRange: NSMakeRange (7, 5)];
BOOL isEqual = [myString isEqualToString: @"Hello, World!" ];
NSMutableString *mutableString = [ NSMutableString stringWithString: @"Hello" ];
[mutableString appendString: @" World!" ];
NSString *string1 = @"Hello" ;
NSString *string2 = @"World" ;
NSString *result = [string1 stringByAppendingFormat: @" %@" , string2];
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:
Share your thoughts in the comments
Please Login to comment...