IOS NSString 用法詳解

來源:互聯網
上載者:User

//NSString 操作均不改變自身值 
//構建字串 
NSString *szTmp = @"A string";      //直接賦值 
szTmp = nil; 
 
int n = 5; 
NSString *szMyString = [NSString stringWithFormat:@"The number is %d",n];   //The number is 5 
[szMyString stringByAppendingFormat:@"%d",22];  //附加字串傳回值:The number is 522 
                                                //但是szMyString本身並沒有改變,其值依然:The number is 5   
[cpp]

//長度與索引字元 
NSLog(@"%d",szMyString.length);                 //字串長度:15 
NSLog(@"%c",[szMyString characterAtIndex:2]);   //返回字元:e 
[cpp]
 
//與c字串相互轉換 
printf("%s\n",[szMyString UTF8String]);         //轉為__strong const char * 
const char *szTmp1 = [szMyString cStringUsingEncoding:NSUTF8StringEncoding]; 
printf("%s\n",szTmp1);                          //轉為__strong const char * 
 
NSLog(@"%@",[NSString stringWithCString:szTmp1 encoding:NSUTF8StringEncoding]); //轉為nsstring 
[cpp]

//字串寫檔案 
NSError *error; 
NSString *szPath = [NSHomeDirectory()           //應用程式沙箱路徑 
                    stringByAppendingPathComponent:@"Documents/testFile.txt"];  //附加路徑地址 
if (![szMyString writeToFile:szPath atomically:YES  //atomically:是否是原子訪問檔案的 
                    encoding:NSUTF8StringEncoding error:&error]) {          //寫入成功返回yes 否則no 
    NSLog(@"Error writing to file :%@",[error localizedDescription]);       //輸出錯誤描述 
    return 1; 

NSLog(@"File write success"); 
[cpp]

//檔案讀字串 
NSString *szInString = [NSString stringWithContentsOfFile:szPath            //讀取檔案資訊 
                        encoding:NSUTF8StringEncoding error:&error]; 
if (!szInString) 

    //失敗 

NSLog(@"%@",szInString);        //成功 
[cpp]

//字串轉為數組 
NSArray *arrayWord = [szMyString componentsSeparatedByString:@" "]; //有空格的拆分為單詞儲存 
NSLog(@"%@",arrayWord); 
[cpp]

//索引子串 
NSString *szSub1 = [szMyString substringToIndex:3];     //0-2,前3個:The 
NSLog(@"%@",szSub1); 
 
NSString *szSub2 = [szMyString substringFromIndex:4];   //4-尾,去掉前4個:number is 5 
NSLog(@"%@",szSub2); 
[cpp]

//範圍索引 
NSRange range; 
range.location = 4;     //從4開始 
range.length = 6;       //6個字元 
NSString *szSub3 = [szMyString substringWithRange:range];       //number 
NSLog(@"%@",szSub3); 
[cpp] view plaincopy
//搜尋與替換 
NSRange rangeSearch = [szMyString rangeOfString:@"is 5"];   //搜尋 
if (rangeSearch.location != NSNotFound) {           //搜尋不到是 NSNotFound 
    //成功:rangeSearch.location;//位置 rangeSearch.length;//長度 

 
NSLog(@"%@",[szMyString stringByReplacingCharactersInRange:rangeSearch      //用位置匹配替換 
                                                withString:@"isn't 10"]); 
 
NSString *szReplaced = [szMyString stringByReplacingOccurrencesOfString:@" " withString:@"*"];  //匹配字串替換 
NSLog(@"%@",szReplaced); 
[cpp]

//改變大小寫 
NSLog(@"%@",[szMyString uppercaseString]);      //大寫 
NSLog(@"%@",[szMyString lowercaseString]);      //小寫 
NSLog(@"%@",[szMyString capitalizedString]);    //首字母大寫 
[cpp]

//比較字串 
NSString *sz1 = @"Hello World!"; 
NSString *sz2 = @"Hello Mom!"; 
if ([sz1 isEqualToString:sz2]) {/*相等*/} 
if ([sz1 hasPrefix:@"Hello"]) {NSLog(@"前部分相等");}        //從頭開始比較 
if ([sz1 hasSuffix:@"d!"]) {NSLog(@"後部分相等");}       //從尾部比較 
[cpp] view plaincopy
//字串轉換數字 
NSString *szNumber = @"3.14"; 
[szNumber intValue]; 
[szNumber boolValue]; 
[szNumber floatValue]; 
[szNumber doubleValue]; 
[cpp] view plaincopy
//可變字串 
NSMutableString *szMuMyString = [NSMutableString stringWithString:@"Hello"]; 
[szMuMyString appendFormat:@"World"];       //字串,改變自身 
[szMuMyString uppercaseString]; 
NSLog(@"%@",szMuMyString); 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.