開發過程中,我們會遇到很多關於字串的操作,它是乙個常用的資料型別,下面對它的用法進行了彙總:
//1、建立常量字串。
nsstring*astring = @"this is a string!";
//2、建立空字串,給予賦值。
nsstring*astring = [[nsstring alloc] init];
astring= @"this is a string!";
nslog(@"astring:%@",astring);
[astring release];
//3、在以上方法中,提公升速度:initwithstring方法
nsstring*astring = [[nsstring alloc] initwithstring:@"this is astring!"];
nslog(@"astring:%@",astring);
[astringrelease];
//4、用標準c建立字串:initwithcstring方法
char*cstring = "this is a string!";
nsstring*astring = [[nsstring alloc] initwithcstring:cstring];
nslog(@"astring:%@",astring);
[astringrelease];
//5、建立格式化字串:佔位符(由乙個%加乙個字元組成)
inti = 1;
intj = 2;
nsstring*astring = [[nsstring alloc] initwithstring:[nsstringstringwithformat:@"%d.this is %i string!",i,j]];
nslog(@"astring:%@",astring);
[astringrelease];
//6、建立臨時字串
nsstring*astring;
astring= [nsstring stringwithcstring:"this is a temporarystring"];
nslog(@"astring:%@",astring);
nsstring*path = @"astring.text";
nsstring*astring = [[nsstring alloc]initwithcontentsoffile:path];
nslog(@"astring:%@",astring);
[astringrelease];
nsstring*astring = [[nsstring alloc] initwithstring:@"this is astring!"];
nslog(@"astring:%@",astring);
nsstring*path =@"astring.text";
[astringwritetofile: path atomically: yes];
[astringrelease];
//用c比較:strcmp函式
charstring1 = "string!";
charstring2 = "string!";
if(strcmp(string1,string2) = = 0)
//isequaltostring方法
nsstring*astring01 = @"this is a string!";
nsstring*astring02 = @"this is a string!";
boolresult = [astring01 isequaltostring:astring02];
nslog(@"result:%d",result);
//compare方法(comparer返回的三種值)
nsstring*astring01 = @"this is a string!";
nsstring*astring02 = @"this is astring!";
boolresult = [astring01 compare:astring02] = =nsorderedsame;
nslog(@"result:%d",result);
//nsorderedsame判斷兩者內容是否相同
nsstring*astring01 = @"this is a string!";
nsstring*astring02 = @"this is a string!";
boolresult = [astring01 compare:astring02] = =nsorderedascending;
nslog(@"result:%d",result);
//nsorderedascending判斷兩物件值的大小(按字母順序進行比較,astring02大於astring01為真)
nsstring*astring01 = @"this is a string!";
nsstring*astring02 = @"this is a string!";
boolresult = [astring01 compare:astring02] = =nsordereddescending;
nslog(@"result:%d",result);
//nsordereddescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)
//不考慮大 小寫比較字串1
nsstring*astring01 = @"this is a string!";
nsstring*astring02 = @"this is a string!";
boolresult = [astring01 caseinsensitivecompare:astring02] = =nsorderedsame;
nslog(@"result:%d",result);
//nsordereddescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)
//如何判斷字串為空
nsstring *urlstring= [urlinput stringvalue];
if(!urlstring)
else
else
}
}
字串的操作 練習三
程式頭部注釋開始 程式的版權和版本宣告部分 檔名稱 字串的操作 練習三 作 者 薛廣晨 完成日期 2011 年 10 月 10 日 版 本號 x1.0 對任務及求解方法的描述部分 輸入描述 問題描述 3,獲取乙個字串在另乙個字串中出現的次數。abkkcdkkefkkskk 思路 1,定義個計數器。2...
字串資料的操作(三)
endswith 判斷字串是否以指定字尾結尾 語法 str.endswith suffix,start,end 用於判斷字串是否以指定字尾結尾,如果以指定字尾結尾返回true,否則返回false。boolean運算 可選引數 start 與 end 為檢索字串的開始與結束位置。預設為整個字串長度 s...
字串操作 靠字串分割字串
字串分解函式。注意strtok比較複雜。要妥善運用!也可以不用strtok函式,但要實現字串靠字串分割比較困難!注意str指向的空間必須是可讀可寫的 如陣列或動態分配的空間 不能為字串常量的指標,因為strtok改變了其中的內容。include include 功能 將str中的字串按照elemon...