Objective-c series-NSMutableString, objective-c
**************************************** ******
NSMutableString is a subclass of NSString. In addition to the parent class method, NSMutableStirng also has some addition, deletion, modification, and replacement methods.
**************************************** ******
Constructor
A newly added constructor:
NSMutableString * mstring = [[NSMutableString alloc] initWithCapacity: 100];
// Note:
// Define a pointer
NSMutableString * mstr;
// Mstr = @ "abc"; // mstr points to a constant, so it is immutable.
Mstr = [[NSString alloc] init]; // This is the open-up immutable string space and immutable string space.
Mstr = [[NSMutableStirng alloc] init]; // This can be changed.
Likewise:
NSString str = [[NSMutableString alloc] init]; // str is also a variable string!
**************************************** ******
// Add
NSMutableString * mstr = [[NSMutableString alloc] init];
// Append a format string to the end of the string mstr
[Mstr appendFormat: @ "[a = % I]-[c = % c]-[s = % s]", 123, 'x', "cstring"];
NSLog (@ "mstr: % @", mstr );
NSLog (@ "mstr length: % lu", [mstr length]);
// Append an oc string to the end of the string mstr, corresponding to strcat in c
[Mstr appendString: @ "-[appendString]"];
NSLog (@ "mstr: % @", mstr );
NSLog (@ "mstr length: % lu", [mstr length]);
**************************************** ******
// Delete
NSMutableString * mstr = [[NSMutableString alloc] initWithString: @ "123abc123abc"];
NSLog (@ "before delete, mstr: % @, mstr length: % lu", mstr, [mstr length]);
// Delete a specified range
[Mstr deleteCharactersInRange: NSMakeRange (9, 3)];
NSLog (@ "after delete, mstr: % @, the length of mstr is: % lu", mstr, [mstr length]);
// Search for a range and delete it based on the range.
// Mstr content: 123abc123
// Locate the abc range in mstr. A struct {3} should be returned}
// Pass this struct to deleteCharactersInRange: Method to delete abc.
[Mstr deleteCharactersInRange: [mstr rangeOfString: @ "abc"];
NSLog (@ "after the second abc deletion, mstr: % @, mstr length: % lu", mstr, [mstr length]);
// Question 1: How to delete substrings that appear multiple times in a string?
// Question 2: How do I replace the substrings that appear multiple times in a string?
// 1. Process cyclically
[Mstr appendString: @ "abc123abc123abc123"];
NSLog (@ "append an abc123. .. After mstr: % @, mstr length: % lu", mstr, [mstr length]);
// There are three abc substrings in mstr
Printf ("NSNotFound = % lu \ n", NSNotFound );
While (1 ){
Nsange range = [mstr rangeOfString: @ "abc"];
If (range. location = NSNotFound ){
Break;
}
[Mstr deleteCharactersInRange: range];
// [Mstr replaceCharactersInRange: [mstr rangeOfString: @ "123"] withString: @ "woca"];
}
NSLog (@ "after abc is deleted in the loop, mstr :%@, the length of mstr is: % lu", mstr, [mstr length]);
// Replace a character in a certain range in the original string with a new string
[Mstr replaceCharactersInRange: NSMakeRange (2, 1) withString: @ "[replace]"];
NSLog (@ "after the replacement method is called, mstr: % @, the length of mstr is: % lu", mstr, [mstr length]);
// Insert a string at a subscript
[Mstr insertString: @ "[insertString]" atIndex: 1];
NSLog (@ "after the insertion method is called, mstr: % @, the length of mstr is: % lu", mstr, [mstr length]);
// Set the string
[Mstr setString: @ "[this is setString]"];
NSLog (@ "after the setString method is called, mstr: % @, the length of mstr is: % lu", mstr, [mstr length]);
**************************************** ******
**************************************** ******