Tutorial by Examples

Simple: NSString *newString = @"My String"; From multiple strings: NSString *stringOne = @"Hello"; NSString *stringTwo = @"world"; NSString *newString = [NSString stringWithFormat:@"My message: %@ %@", stringOne, stringTwo]; Usi...
NSString has a length property to get the number of characters. NSString *string = @"example"; NSUInteger length = string.length; // length equals 7 As in the Splitting Example, keep in mind that NSString uses UTF-16 to represent characters. The length is actually just the numbe...
To convert a String to uppercase, use uppercaseString: NSString *myString = @"Emphasize this"; NSLog(@"%@", [myString uppercaseString]; // @"EMPHASIZE THIS" To convert a String to lowercase, use lowercaseString: NSString *myString = @"NORMALIZE this"; N...
Strings are compared for equality using isEqualToString: The == operator just tests for object identity and does not compare the logical values of objects, so it can't be used: NSString *stringOne = @"example"; NSString *stringTwo = [stringOne mutableCopy]; BOOL objectsAreIdentical =...
To combine an NSArray of NSString into a new NSString: NSArray *yourWords = @[@"Objective-C", @"is", @"just", @"awesome"]; NSString *sentence = [yourWords componentsJoinedByString:@" "]; // Sentence is now: @"Objective-C is just awesome&quo...
// decode NSString *string = [[NSString alloc] initWithData:utf8Data encoding:NSUTF8StringEncoding]; // encode NSData *utf8Data = [string dataUsingEncoding:NSUTF8StringEncoding]; Some supported encodings are: NSASCIIStringEncoding NSUTF8StringEnc...
You can split a string into an array of parts, divided by a separator character. NSString * yourString = @"Stack,Exchange,Network"; NSArray * yourWords = [yourString componentsSeparatedByString:@","]; // Output: @[@"Stack", @"Exchange", @"Network&quot...
To search if a String contains a substring, do the following: NSString *myString = @"This is for checking substrings"; NSString *subString = @"checking"; BOOL doesContainSubstring = [myString containsString:subString]; // YES If targeting iOS 7 or OS X 10.9 (or earlier)...
To convert NSString to const char use -[NSString UTF8String]: NSString *myNSString = @"Some string"; const char *cString = [myNSString UTF8String]; You could also use -[NSString cStringUsingEncoding:] if your string is encoded with something other than UTF-8. For the reverse path use...
NSString *someString = @" Objective-C Language \n"; NSString *trimmedString = [someString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //Output will be - "Objective-C Language" Method stringByTrimmingCharactersInSet returns a new str...
The NSString formatting supports all the format strings available on the printf ANSI-C function. The only addition made by the language is the %@ symbol used for formatting all the Objective-C objects. It is possible to format integers int myAge = 21; NSString *formattedAge = [NSString stringWith...
// myString is "hi" NSMutableString *reversedString = [NSMutableString string]; NSInteger charIndex = [myString length]; while (charIndex > 0) { charIndex--; NSRange subStrRange = NSMakeRange(charIndex, 1); [reversedString appendString:[myString substringWithRange:subS...

Page 1 of 1