Mutable vs. Immutable Strings
NSMutableString – Should be used when you are physically changing the value of an existing string, without completely discarding the old value (i.e. adding a character to the beginning or end, modifying a character in the middle etc).
NSString – Can never be changed after it has been created, only overwritten with a completly new string. Most NSStrings (including @”Constant Strings”) are autoreleased. NStrings occupy a fixed number of bytes in memory and are the most efficient.
NSLocalizedString – Return a localized version of a string
Guides
Create A NSString
//Create a string
NSString *MyStringName1 = @"Hello";
//Create new NSString from another string
NSString *MyStringName2 = [MyStringName1 stringByAppendingString:@", world!"];
Create NSString From Part Of NSString
NSString *source = @"0123456789";
NSString *firstFour = [source substringToIndex:4];
// firstFour is @"0123"
NSString *allButFirstThree = [source substringFromIndex:3];
// allButFirstThree is @"3456789"
NSRange twoToSixRange = NSMakeRange(2, 4);
NSString *twoToSix = [source substringWithRange:twoToSixRange];
// twoToSix is @"2345"
Modifying a NSString
Use stringByAppendingString:
This adds a string to an existing one and returns a new string. (Underneath NSString copies the old string to a new larger memory location, adds the argument and returns the new string.
NSString *MyString;
MyString = [[MyArray objectAtIndex:0] stringByAppendingString:@"1234"];
MyString = [MyString stringByAppendingString:@"5678"];
Add Variable to String
TempString = [NSString stringWithFormat:@"%d days ago ", days]];
if ([MyStringName length] > 0)
Convert String To Variable
MyIntValue = [MyString intValue]; //Will equal 0 if string is not numeric
MyBoolValue = [MyString boolValue]; //Converts MyString to BOOL
NSString intialisation with NSMutableString
MyNSString = [NSString stringWithString:MyNSMutableString];
//Doing this instead means you can now release or change MyNSMutableString without affecting MyNSString
[MyNSMutableString release];
Does String Equal
if ([MyStringName isEqual:@"SomeText"])
(Don’t use “==”)
Does String Contain
if ([MyStringToCheck rangeOfString:@".mov" options:(NSCaseInsensitiveSearch)].location != NSNotFound)
{
//Yes it does contain string
}
Modify The End Of A String
NSRange EndRange = [MediaFileName rangeOfString:@".MOV"];
if (EndRange.length > 0)
{
MediaFileName = [MediaFileName substringToIndex:EndRange.location];
MediaFileName = [MediaFileName stringByAppendingString:@".mov"];
}
Get Characters Between Markers Within String
NSString *SourceString = @"[START]12345[END]";
NSString *SubString;
NSRange StartRange = [SourceString rangeOfString:@"[START]"];
NSRange EndRange = [SourceString rangeOfString:@"[END]"];
if ((StartRange.length > 0) && (EndRange.length > 0))
{
NSRange SubStringRange = NSMakeRange((StartRange.location + StartRange.length), (EndRange.location - (StartRange.location + StartRange.length)));
SubString = [SourceString substringWithRange:SubStringRange];
}
else
{
SubString = @"";
}
Convert Strings
NSString *MyNewString1 = [MyOtherString lowercaseString];
NSString *MyNewString2 = [MyOtherString uppercaseString];
NSString *MyNewString2 = [MyOtherString capitalizedString];
Replace Characters In String
CellMainLabel = [CellMainLabel stringByReplacingOccurrencesOfString:@"THE " withString:@""];
Get Character In String
if ([MyString characterAtIndex:1] == @"2")
Remove Whitespace From String
MyString = [MyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Convert NSString to ASCII value
NSString *MyString = @"A";
int asciiCode = [MyString characterAtIndex:0]; //=65
Convert ASCII value to NSString
int asciiValue = 65;
NSString *string = [NSString stringWithFormat:@"%c", asciiValue]; //="A"
NSString Being Released
NSStrings are autoreleased. If you are using one as a global object for instance you often need to retain it when setting its value to stop this happening:
[MyString retain];
or if you are using ARC for memory management:
@interface ...
{
NSString *MyString;
}
@property (nonatomic, retain) NSMutableString *characterAtIndex;
@synthesize MyString;
Then use it like this:
self.MyString -
Create String From String
name = [NSString stringWithString:name];