002-Variable string (nsmutablestring)

Source: Internet
Author: User

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)

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.