Objective-C nsmutablestring variable string

Source: Internet
Author: User

Nsstring itself does not allow content and String Length modification. If you want to modify the content, you can use nsmutablestring.

Nsmutablestring is a subclass of nsstring. Therefore, all nsstring methods are applicable to nsmutablestring.

Nsstring * str1 = @ "Welcome, same! "; Nsstring * str2, * str3; nsmutablestring * mstr; nsange range; mstr = [nsmutablestring stringwithstring: str1]; nslog (@" % @ ", mstr); [mstr insertstring: @ "back" atindex: 9]; nslog (@ "% @", mstr); [mstr insertstring: @ "how are you" atindex: [mstr length]; nslog (@ "% @", mstr); [mstr appendstring: @ "in there? "]; Nslog (@" % @ ", mstr); [mstr deletecharactersinrange: nsmakerange (29,9)]; nslog (@" % @ ", mstr ); range = [mstr rangeofstring: @ "how are you? "]; If (range. location! = Nsnotfound) {[mstr deletecharactersinrange: range]; nslog (@ "% @", mstr);} [mstr setstring: @ "Welcome, Sam! "]; Nslog (@" % @ ", mstr );

Running result:

Welocome, Sam!
Welocome, back Sam!
Welocome, back Sam! How are you
Welocome, back Sam! How are you in there?
Welocome, back Sam! How are you?
Welocome, back Sam!

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.