iOS 中幾種簡單常用的操作

2021-06-28 19:52:10 字數 3277 閱讀 9674

1、判斷陣列中是否存在某元素

boolisvalue = [keyarray containsobject:q"aaa"];

2、把字串按逗號隔開,並儲存到陣列:

nsarray*keyarray=[[nsarray alloc] init];

keyarray=[@"冬瓜,西瓜,南瓜,苦瓜,絲瓜"componentsseparatedbystring:@","];

3、把陣列中的取出來,拼成用逗號隔開的字串:

nsstring*n=[keyarray componentsjoinedbystring:@","];

4、 nsmutablearray轉化成nsarray

nsarray*phonea=[[nsarray alloc] init];

nsmutablearray*phonearrayss=[[nsmutablearray alloc] init];

phonea=[phonearrayssmutablecopy];

5、獲取本地時間(大寫hh獲取24小時制的)

nsdateformatter*formatter = [[nsdateformatter alloc] init];

[formattersetdateformat:@"yyyy-mm-dd hh:mm:ss"];

nsstring*timestr=[formatter stringfromdate: [nsdate date]];

6、去除nsstring中的空格

nscharacterset *whitespace =[nscharacterset whitespaceandnewlinecharacterset ];

nsstring * username = [musernamefield stringvalue];

username = [usernamestringbytrimmingcharactersinset :whitespace];

7、需要判斷的地方:

isnetworking *isnetwork = [[isnetworking alloc] init];

bool isnets=[isnetwork isnetworkreachable];

if(isnets==no)

else

8、追加字元:

nsmutablestring*string = [[nsmutablestring alloc] init];

string=@「你好」;

9、字串替換:把info中所有的《都替換成#

nsstring*stroneintro=[info stringbyreplacingoccurrencesofstring:@"<"

withstring:@"#"];

10、字串比較:

boolistrue=[@"nob"isequaltostring:@"mob"]

11、不考慮大小寫比較字串

nsstring*astring01 = @"this is a string!";

nsstring*astring02 = @"this is a string!";

boolresult = [astring01 caseinsensitivecompare:astring02] = =nsorderedsame;

12、改變字串的大小寫

nsstring*string1 = @"a string";

nsstring*string2 = @"string";

nslog(@"string1:%@",[string1uppercasestring]);//大寫

nslog(@"string2:%@",[string2lowercasestring]);//小寫

nslog(@"string2:%@",[string2capitalizedstring]);//首字母大小

13、在串中搜尋子串

nsstring*string1 = @"this is a string";

nsstring*string2 = @"string";

nsrangerange = [string1 rangeofstring:string2];

intlocation = range.location;

intleight = range.length;

nsstring*astring = [[nsstring alloc] initwithstring:                

[nsstringstringwithformat:@"location:%i,leight:%i",location,leight]];

nslog(@"astring:%@",astring);

[astringrelease];

14、 抽取子串

//-substringtoindex: 從字串的開頭一直擷取到指定的位置,但不包括該位置的字元

nsstring*string1 = @"this is a string";

nsstring*string2 = [string1 substringtoindex:3];

nslog(@"string2:%@",string2);

//-substringfromindex:以指定位置開始(包括指定位置的字元),幷包括之後的全部字元

nsstring*string1 = @"this is a string";

nsstring*string2 = [string1substringfromindex:3];

nslog(@"string2:%@",string2);

//-substringwithrange: //按照所給出的位置,長度,任意地從字串中擷取子串

nsstring*string1 = @"this is a string";

nsstring*string2 = [string1 substringwithrange:nsmakerange(0,4)];

nslog(@"string2:%@",string2);

15、判斷字串內是否還包含別的字串(字首,字尾)

//01:檢查字串是否以另乙個字串開頭- (bool) hasprefix: (nsstring *)astring;

nsstring*string1 = @"nsstringinformation.txt";

[string1hasprefix:@"nsstring"] = = 1 ? nslog(@"yes") :nslog(@"no");

[string1hassuffix:@".txt"] = = 1 ? nslog(@"yes"): nslog(@"no");

iOS路徑的幾種操作

nsstring path users tarena documents core ios 1.加路徑內容,自動加上 nsstring model and storage nslog path newpath 2.拆分路徑 nsarray conponents newpath pathcompone...

Python讀取檔案常用的幾種簡單小操作

1.普通讀取f open 開啟檔案,迴圈讀取 def read filename f open filename,r for line in f print line 2.with上下文讀取def read filename with open filename,r as f for line in...

iOS開發中runtime常用的幾種方法示例總結

前言 objective c runtime是乙個實現objective c語言的c庫。它是一門編譯型語言 也是一門動態型的語言 這裡強調下oc是靜態型別語言 之前沒接觸runtime的時候也不覺著它有多重要,接觸之後才發現其實runtime挺強大的。就拿我們在ios開發中所使用的oc程式語言來講,...