Objective-C variable string operation

Source: Internet
Author: User

 

# Import <Foundation/nsstring. h>
# Import <Foundation/nfutoreleasepool. h>

Int main (INT argc, const char * argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Nsstring * str1 = @ "this is a example .";
Nsmutablestring * mstr = [[nsmutablestring alloc] init];
Nsange substr;
// Create a variable string
Mstr = [nsmutablestring stringwithstring: str1];
Nslog (@ "% @", mstr );
// Insert characters
[Mstr insertstring: @ "very easy" atindex: 10];
Nslog (@ "% @", mstr );
// Delete some characters
[Mstr deletecharactersinrange: nsmakerange (10, 5)];
Nslog (@ "% @", mstr );
// Search for and delete
Substr = [mstr rangeofstring: @ "easy"];
If (substr. location! = Nsnotfound ){
[Mstr deletecharactersinrange: substr];
Nslog (@ "% @", mstr );
}
// Reset the string
[Mstr setstring: @ "this is string a"];
// Replace the string
[Mstr replacecharactersinrange: nsmakerange (15, 1) withstring: @ "BBB"];
Nslog (@ "% @", mstr );
// Search for and replace the first one
Nsstring * search = @ "this is ";
Nsstring * replace = @ "an example ";
Substr = [mstr rangeofstring: Search];
If (substr. location! = Nsnotfound ){
[Mstr replacecharactersinrange: substr withstring: replace];
Nslog (@ "% @", mstr );
}
// Search for all matched items and replace them
Search = @ "";
Replace = @ "X ";
Substr = [mstr rangeofstring: Search];
While (substr. location! = Nsnotfound ){
[Mstr replacecharactersinrange: substr withstring: replace];
Substr = [mstr rangeofstring: Search];
}
Nslog (@ "% @", mstr );
[Pool drain];
Return 0;
}

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.