閱讀446 返回首頁    go 魔獸


Object-C中的字符串對象2-可變字符串

//
//  main.m
//  字符串-可變字符串
//
//  Created by zhangqs008 on 14-2-13.
//  Copyright (c) 2014年 zhangqs008. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        NSString *str1=@"this is string A";
        NSString *search;
        NSString *replace;
        NSMutableString *mutableStr;
        NSRange subStr;
        
        //01.從不可變字符串創建可變字符串
        mutableStr=[NSMutableString stringWithString:str1];
        NSLog(@"01 %@",mutableStr);
        
        //02.插入字符,注意:是直接修改的字符串對象,無返回值
        [mutableStr insertString:@" mutable" atIndex:7];
        NSLog(@"02 %@",mutableStr);
        
        //03.插入末尾進行有效拚接
        [mutableStr insertString:@" and string B" atIndex:[mutableStr length]];
        NSLog(@"03 %@",mutableStr);
        
        //04.直接用appendString
        [mutableStr appendString:@" and string C"];
        NSLog(@"04 %@",mutableStr);
        
        //05.根據範圍刪除字符串 deleteCharactersInRange(從*開始,刪除*個字符)
        [mutableStr deleteCharactersInRange:NSMakeRange(16,13)];
        NSLog(@"05 %@",mutableStr);
        
        //06.查找然後將其刪除
        subStr=[mutableStr rangeOfString:@"string B and "];
        if(subStr.location!=NSNotFound)
        {
            [mutableStr substringWithRange:subStr];
            NSLog(@"06 %@",mutableStr);
            
        }
        
        //07.直接設置為可變字符串
        [mutableStr setString:@"this is string A"];
        NSLog(@"07 %@",mutableStr);
        
        //08.替換一些字符
        [mutableStr replaceCharactersInRange:NSMakeRange(8, 8) withString:@"a mutable string"];
        NSLog(@"08 %@",mutableStr);
        
        //09.查找和替換
        search=@"this is";
        replace=@"An example of";
        
        subStr=[mutableStr rangeOfString:search];
        if(subStr.location!=NSNotFound)
        {
            [mutableStr replaceCharactersInRange:subStr withString:replace];
            NSLog(@"09 %@",mutableStr);
        }
        
        //10.查找和替換所有匹配項
        search=@"a";
        replace=@"X";
        subStr=[mutableStr rangeOfString:search];
        while (subStr.location!=NSNotFound) {
            [mutableStr replaceCharactersInRange:subStr withString:replace];
            subStr=[mutableStr rangeOfString:search];
        }
        NSLog(@"10 %@",mutableStr);
        
        //也可以這樣寫
        [mutableStr replaceOccurrencesOfString:search withString:replace options:nil range:NSMakeRange(0, [mutableStr length])];
        
        NSLog(@"10 %@",mutableStr);
        
        
        
    }
    return 0;
}

輸出結果:

2014-02-13 20:38:30.074 05.字符串-可變字符串[997:303] 01 this is string A

2014-02-13 20:38:30.076 05.字符串-可變字符串[997:303] 02 this is mutable string A

2014-02-13 20:38:30.079 05.字符串-可變字符串[997:303] 03 this is mutable string A and string B

2014-02-13 20:38:30.079 05.字符串-可變字符串[997:303] 04 this is mutable string A and string B and string C

2014-02-13 20:38:30.080 05.字符串-可變字符串[997:303] 05 this is mutable string B and string C

2014-02-13 20:38:30.080 05.字符串-可變字符串[997:303] 06 this is mutable string B and string C

2014-02-13 20:38:30.081 05.字符串-可變字符串[997:303] 07 this is string A

2014-02-13 20:38:30.081 05.字符串-可變字符串[997:303] 08 this is a mutable string

2014-02-13 20:38:30.081 05.字符串-可變字符串[997:303] 09 An example of a mutable string

2014-02-13 20:38:30.082 05.字符串-可變字符串[997:303] 10 An exXmple of X mutXble string

2014-02-13 20:38:30.082 05.字符串-可變字符串[997:303] 10 An exXmple of X mutXble string

Program ended with exit code: 0



最後更新:2017-04-03 12:55:06

  上一篇:go Android 關於 OnScrollListener 事件順序次數的簡要分析
  下一篇:go SQL Server 2008 R2用戶&#39;sa&#39;登錄失敗(錯誤18456)