Nsstring form -- variable string -- alter Method

Source: Internet
Author: User

We have explained the first three methods of string deformation. Now we are talking about the last method. The method is similar to the method of overwriting, but it is slightly different from the overwrite method. It is not directly covered by another definition, but can be overwritten by modifying the position and length of the original string.

 

The following methods are involved:

Replacecharactersinrange:It means to modify the original string through the position and length returned by nsmakerange.

 

The following is a simple example:

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSMutableString *str = [NSMutableString new];        [str setString:@"0123456789"];        NSLog(@"\n%@", str);                [str replaceCharactersInRange:NSMakeRange(3, 5) withString:@"abcdefg"];        //通过一个NSMakeRange返回的位置和长度, 把abcdefg这个字符串从第三个, 长度为5的字符串改成abcdefg.        //通俗点说就是把34567这五个改成abcdefg, 而后面的89就会跟在abcdefg后面.                NSLog(@"\n%@", str);    }    return 0;}

Output results before modification:

2014-10-11 17:04:48.799 AlterNSMutableString[1647:303] str = 0123456789Program ended with exit code: 0

Output result after modification:

2014-10-11 17:04:48.801 AlterNSMutableString[1647:303] str = 012abcdefg89Program ended with exit code: 0

 

Nsstring form -- variable string -- alter Method

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.