objective c中的NSString操作

2021-08-31 15:29:30 字數 3637 閱讀 6793

//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 a string!"];

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

[astring release];

//4、用標準c建立字串:initwithcstring方法

char *cstring = "this is a string!";

nsstring *astring = [[nsstring alloc] initwithcstring:cstring];

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

[astring release];

//5、建立格式化字串:佔位符(由乙個%加乙個字元組成)

int i = 1;

int j = 2;

nsstring *astring = [[nsstring alloc] initwithstring:[nsstring stringwithformat:@"%d.this is %i string!",i,j]];

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

[astring release];

//6、建立臨時字串

nsstring *astring;

astring = [nsstring stringwithcstring:"this is a temporary string"];

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

nsstring *path = @"astring.text";

nsstring *astring = [[nsstring alloc] initwithcontentsoffile:path];

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

[astring release];

nsstring *astring = [[nsstring alloc] initwithstring:@"this is a string!"];

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

nsstring *path = @"astring.text";    

[astring writetofile: path atomically: yes];

[astring release];

//用c比較:strcmp函式

char string1 = "string!";

char string2 = "string!";

if(strcmp(string1, string2) = = 0)

//isequaltostring方法    

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

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

bool result = [astring01 isequaltostring:astring02];

nslog(@"result:%d",result);

//compare方法(comparer返回的三種值)    

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

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

bool result = [astring01 compare:astring02] = = nsorderedsame;    

nslog(@"result:%d",result);    

//nsorderedsame 判斷兩者內容是否相同

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

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

bool result = [astring01 compare:astring02] = = nsorderedascending;    

nslog(@"result:%d",result);

//nsorderedascending 判斷兩物件值的大小(按字母順序進行比較,astring02大於astring01為真)

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

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

bool result = [astring01 compare:astring02] = = nsordereddescending;    

nslog(@"result:%d",result);     

//nsordereddescending 判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)

//不考慮大 小寫比較字串1

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

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

bool result = [astring01 caseinsensitivecompare:astring02] = = nsorderedsame;    

nslog(@"result:%d",result);     

//nsordereddescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)

//如何判斷字串為空

nsstring *urlstring = [urlinput stringvalue];

if (!urlstring) else else

} } 

convert nsstring to int

1nsstring*anumberstring =@"123";

2inti = [anumberstring intvalue];

convert int to nsstring

1intanumber = 123;

2nsstring*astring = [nsstringstringwithformat:@"%d", anumber];

Objective C中的快取

nscache可以設定數量限制,通過countlimit與 totalcostlimit來限制cache的數量或者限制cost。當快取的數量超過countlimit,或者cost之和超過totalcostlimit,nscache會自動釋放部分快取。例子如下 可以看到,cache中只保留了最新的30...

Objective C中的callback之一

oc中的callback有四種型別 下面是第一種 nsrunloop bnrlogger.h tocrunloopa import inte ce bnrlogger nsobject property nonatomic nsdate lasttime nsstring lasttimerstri...

Objective C中的繼承

1.父類自身也可以有父類,沒有父類的類位於類層次的最頂層,稱為根類 父類也可以被稱為超類 2.繼承中,父類的非私有例項變數和方法都會成為新類定義的一部分。子類可以直接訪問這些方法和例項變數,就像在類定義中直接定義了這些子類一樣。注意 在子類使用例項變數,必須在介面部分宣告,而不是在實現部分宣告。在實...