Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
See the code:
[CPP]View Plaincopy print?
- //
- Main.m
- Foundation5-nsmutablestring
- //
- Created by MJ on 13-4-5.
- Copyright (c) 2013 itcast. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #pragma the creation of the mark mutable string
- void Stringcreate () {
- //Pre-allocate 10 words of storage space
- nsmutablestring *str = [[Nsmutablestring alloc] initwithcapacity:10];
- //Set string contents
- [Str setstring:@"1234"];
- //Splicing a string
- [Str appendstring:@"567"];
- //Stitching strings
- [Str appendformat:@"Age was%i and height is%.2f", 1.55f];
- //Replace string
- Nsrange range = [str rangeofstring:@"height"]; Find the position of the string height
- [Str replacecharactersinrange:range withstring:@"no"];
- //Insert String
- [Str insertstring:@"abc" atindex:2];
- //Delete string
- range = [str rangeofstring:@"age"];
- [Str Deletecharactersinrange:range];
- NSLog (@"%@", str);
- //Release Object
- [STR release];
- }
- int main (int argc, const char * argv[])
- {
- @autoreleasepool {
- Stringcreate ();
- }
- return 0;
- }
nsmutablestring usage of iOS