代码改变世界

object-c中子字符串(NSMutableString)基本操作

2013-04-23 22:14  甘超波  阅读(460)  评论(0编辑  收藏  举报

 

 

void subString(){
    NSMutableString *string=[NSMutableString stringWithString:@"ABCED"];
    [string setString:@"abcedg"];//NSString是不变字符串类,NSMutableString继承NSSting,经过包装NSMuTableString是可变的
    NSLog(@"%@",string);
    //是在字符串后面追加字符串
    [string setString:@"abcedg"];
    [string appendString:@"FF"];
    NSLog(@"%@",string);
    
    [string setString:@"abcedg"];
    [string appendFormat:@"age is %i",12];
    NSLog(@"%@",string);
    //替换字符串
    [string setString:@"abcedg"];
        //指定字符串替换,一般和下面的方法一起使用
//    NSRange rang=[string rangeOfString:@"ced"];
    [string replaceCharactersInRange:NSMakeRange(2, 3) withString:@"ABC"];
    
    //字符串的插入
       [string setString:@"abcedg"];
    [string insertString:@"88" atIndex:2];//在原来的字符串索引位置插入字符串
    NSLog(@"%@",string);
    
    //删除指定字符串
    [string setString:@"abcedg"];
   //NSRange rang=[string rangeOfString:@"bc"];
    [string deleteCharactersInRange:NSMakeRange(1, 2)];
    NSLog(@"%@",string);

}



int main(int argc, const char * argv[])
{
    @autoreleasepool {
        subString();
 
    }
    return 0;
}

 其中结果

2013-04-23 07:11:56.425 字符串03[1512:303] abcedg
2013-04-23 07:11:56.432 字符串03[1512:303] abcedgFF
2013-04-23 07:11:56.433 字符串03[1512:303] abcedgage is 12
2013-04-23 07:11:56.434 字符串03[1512:303] ab88cedg
2013-04-23 07:11:56.436 字符串03[1512:303] aedg