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!