Replace the string (replace the string content you want to replace directly)/delete (delete the specified location/delete the location you want to delete)/

Source: Internet
Author: User

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

@autoreleasepool {

Nsmutablestring *mustr1=[[nsmutablestring Alloc]init];

Nsmutablestring *mustr2=[nsmutablestring stringwithformat:@ "Hello"];

Nsmutablestring *mustr3=[[nsmutablestring Alloc]init];

NSString *[email protected] "welcom to OC";

nsmutablestring *[email protected] "welcom to OC"; This is wrong--you cannot assign a string constant directly to Nsmutablestring

Assign value

Mustr1=[nsmutablestring STRINGWITHSTRING:STR];

NSLog (@ "mustr1=%@", MUSTR1);

Replace string

Nsrange rang2=[mustr1 rangeofstring:@ "OC"];

[Mustr1 replacecharactersinrange:rang2 withstring:@ "IOS"];

NSLog (@ "mustr1=%@", MUSTR1);

Insert

[Mustr1 insertstring:@ "student" atindex:6]; Specify position 6 Insert string

[Mustr2 appendstring:@ "teacher!"]; Insert string at end

[Mustr3 appendformat:@ "hey!%@", str]; Formatting the insertion string

NSLog (@ "mustr1=%@", MUSTR1);

NSLog (@ "mustr2=%@", MUSTR2);

NSLog (@ "mustr3=%@", MUSTR3);

Specify location Delete

[Mustr1 Deletecharactersinrange:nsmakerange (7, 8)]; Remove 8 characters at position 7

NSLog (@ "mustr1=%@", MUSTR1);

Find Delete

Nsrange rang1=[mustr2 rangeofstring:@ "Teacher"];

if (rang1.location!=nsnotfound) {

[Mustr2 deletecharactersinrange:rang1];

}

NSLog (@ "mustr2=%@", MUSTR2);

Re-assign Value

[Mustr2 SETSTRING:STR];

NSLog (@ "mustr2=%@", MUSTR2);

}

return 0;

}

Replace the string (replace the string content you want to replace directly)/delete (delete the specified location/delete the location you want to delete)/

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.