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



NSMutableString is a subclass of NSString. So any method which can take an NSString can also take an NSMutableString.

Create A NSMutableString


	NSMutableString *MyStringName;
	MyStringName = [[NSMutableString alloc] init];

	//Remember to release
	[MyStringName release];

Set NSMutableString Value

    NSMutableString *MyStringName = [NSMutableString stringWithFormat:@"some text"];
    [MyStringName setString:@"new text"];    //Actually set the string value

    MyStringName = SomeOtherNSMutableString;
    //This sets the pointer to another string object  - don't use it!!  This is a classic cause of bugs where you think you are writing text values but actually you are just updating the pointer, made worse by this not having requried "alloc] init]" to have been used causing unexplainable null value errors when you do decide to use setString!

Append To String

	[MyStringName setString:@"abcd"];
	[MyStringName appendString:SomeOtherStringName];

Does NSMutableString Equal

    //Test for is equal
    if ([MyStringName isEqualToString:@"SomeText"])

    //Test for NOT equal
    if (![OriginalMediaFileName isEqualToString:@""])

Convert Strings

	myString = [myString lowercaseString];
	myString = [myString capitalizedString];

Add Variable to String

    [TempString setString:[NSString stringWithFormat:@"%d days ago ", days]];