Nsstring and nsmutablestring operations: adding, deleting, and modifying strings

Source: Internet
Author: User

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;
}

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.