SONIC3D
8/4/2012 - 5:31 PM

NSMutableString Code Snippet

NSMutableString Code Snippet

        // life cycle
        NSMutableString *s1 = [NSMutableString stringWithCapacity:10];
        NSMutableString *s2 = [[NSMutableString alloc] initWithCapacity:10];
        [s2 release];
        // fill string
        [s1 appendFormat:@"%@ Objective-C!", @"Hello"];
        NSLog(@"%@", s1);
        // append string
        [s1 appendString:@" Glad to be here."];
        NSLog(@"%@", s1);
        // delete characters
        [s1 deleteCharactersInRange:NSMakeRange(s1.length-6, 6)];
        NSLog(@"%@", s1);
        // insert
        [s1 insertString:@" alive!" atIndex:s1.length];
        NSLog(@"%@", s1);
        // replace
        [s1 replaceCharactersInRange:NSMakeRange(19, 4) withString:@"Excited"];
        NSLog(@"%@", s1);
        // replace occurrence
        [s1 replaceOccurrencesOfString:@"Objective-C" withString:@"World" options:NSCaseInsensitiveSearch range:NSMakeRange(0, s1.length)];
        NSLog(@"%@", s1);
        // replace the entire string
        [s1 setString:@"This is a new String"];
        NSLog(@"%@", s1);