Nsstring is a string that defines a fixed size. nsmutablestring is variable and can be used to add, delete, modify, or insert strings.
Source codeStrings. m
-----------------------------------
# Import <Foundation/Foundation. h>
Int main (INT argc, const char * argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
// Insert code here...
Nsstring * artist = @ "Underworld ";
Nslog (@ "The \" % @ \ "length is % d", artist, [artist length]);
Nsstring * substring = [artist substringfromindex: 5];
Nslog (@ "from index 5 to end that substring is \" % @ \ "", substring );
Nsange range = [artist rangeofstring: @ "W"];
Nslog (@ "range. Location = % d, which is the index location of the letter \" w \ "", range. Location );
Nslog (@ "range. Length = % d, which is the length of the letter \" w \ ", range. Length );
Nslog (@"------------------------------");
Nsstring * Song = @ "beautiful New World, wooyun ";
Nsange range1 = [Song rangeofstring: @ ","];
Nsstring * songname = [Song substringtoindex: range1.location];
Nsstring * songauthor = [Song substringfromindex: range1.location + range1.length]; // If range1.length is not added, the words include commas.
Nslog (@ "Song: % @", SONG );
Nslog (@ "\", \ "Location: % d", range1.location );
Nslog (@ "song name: % @", songname );
// Printf ("song name: % s \ n", [songname utf8string]); // correct
// Nslog (@ "song name: % @", [songname utf8string]); // Error
Nslog (@ "Author: % @", songauthor );
Nslog (@"---------------------------------");
Nslog (@ "variable string class nsmultablestring Demo :");
Nsmutablestring * movie = [[nsmutablestring alloc] initwithstring: @ "Transformers 2"];
Nslog (@ "% @", movie );
[Movie appendstring: @ ", paramount film"];
Nslog (@ "% @. Method appendstring", movie );
Nsange range2 = [Movie rangeofstring: @ "Paramount film"];
[Movie replacecharactersinrange: range2 withstring: @ "Paramount movie company"]; // not range2.location
Nslog (@ "% @. Method replacecharactersinrange", movie );
[Movie deletecharactersinrange: [Movie rangeofstring: @ "company"];
Nslog (@ "% @. deletecharactersinrange", movie );
Range2 = [Movie rangeofstring: @ "Transformers"];
[Movie insertstring: @ "sci-fi movie:" atindex: range2.location];
Nslog (@ "% @. Method insertstring", movie );
[Movie release]; // do not forget the release for objects using alloc
[Pool drain];
Return 0;
}