In Objective-C, a string can be created using the NSString class.
There are various ways to create a string in Objective-C, some common examples are:
1. Creating a string with a literal:
``objective-c
NSString *myString = @"Hello World";
stringWithFormat:
2. Using the method:
objective-c
NSString *name = @"John";
NSString *myString = [NSString stringWithFormat:@"Hello, %@!", name];
NSLog(@"%@", myString); // outputs "Hello, John!"
objective-cinitWithFormat:
3. Using themethod:
NSString *name = @"Mary";
NSString *myString = [[NSString alloc] initWithFormat:@"Hello, %@!", name];
NSLog(@"%@", myString); // outputs "Hello, Mary!"
4. Concatenating strings:
objective-cNSString *firstName = @"John";
NSString *lastName = @"Doe";
NSString *myString = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
NSLog(@"%@", myString); // outputs "John Doe"
5. Using a character array:
objective-cchar cString[] = "Hello World";
NSString *myString = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
NSLog(@"%@", myString); // outputs "Hello World"
`
Note that Objective-C also supports mutable strings, which can be modified after creation using the
NSMutableString` class.