Objective-c series-NSMutableString, objective-c

Source: Internet
Author: User

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]);

**************************************** ******

 

**************************************** ******

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.