The NSString class is an immutable class, that is, once the NSString object is created, the sequence of characters contained in the object is immutable until the object is destroyed.
The Nsmutablestring object represents a string with a variable sequence of characters, and Nsmutablestring is a subclass of NSString, and the NSString class contains methods that can be used directly by nsmutablestring. Nsmutablestring objects can also be used directly as NSString objects.
-(void)appendString:(
NSString *
)
aString
//Add a fixed string after a string, and the newly generated string is also saved at the address of the original string.
-(void)AppendFormat:(
NSString *
)
format
,
...
//Add a string with a variable after the string
-(void)insertstring:(
NSString *
)
aString
Atindex:(
NSUInteger
)
anIndex
//Insert a string at the specified location
-(void)Deletecharactersinrange:(
NSRange
)
aRange
//Delete a string between a range
-(void)Replacecharactersinrange:(
NSRange
)
aRange
withstring:(
NSString *
)
aString
Replace a string with a string that has a range set
main.m//03-Variable string (nsmutablestring)////Created by Bao Shirong on 15/8/19.//Copyright (c) 2015 Bao Shirong. All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {nsstring* book = @ "<<crazy IOS book>>"; Create a Nsmutablestring object nsmutablestring* str = [nsmutablestring stringwithstring: @ "Hello"]; NSLog (@ "Present address is:%p", str); Append fixed string//string The character sequence itself has changed, so there is no need to re-assign [str appendString: @ ", ios!"]; NSLog (@ "Present address is:%p", str); NSLog (@ "%@", str); The string//string appended with the variable has itself changed, so there is no need to re-assign [str appendformat: @ "%@ is a good book." NSLog (@ "%@", str); Inserting a string//string in the specified position contains a sequence of characters that itself has changed, so there is no need to re-assign [str insertstring: @ "fkit.org" atindex:6]; NSLog (@ "%@", str); Delete 12 characters from the beginning of the position [str deletecharactersinrange:nsmakerange (6, 12)]; NSLog (@ "%@", str); Replace the string from position 6 to position 9 with objective-c [str replacecharactersinrange:nsmakerange (6, 9) withstring: @ "objective-c"]; NSLog (@ "%@", str); } return 0;}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
002-Variable string (nsmutablestring)