Object C中的字串物件1 不可變字串

2021-06-20 11:18:36 字數 3486 閱讀 8504

#import int main(int argc, const char * argv)

else

//05.驗證乙個字串是否小於,等於或大於另一字串

compareresult=[str1 compare:str2];

if(compareresult==nsorderedascending)

else if(compareresult==nsorderedsame)

else

//06.將字串轉為大寫

temp=[str1 uppercasestring];

nslog(@"字串大寫:%@",temp);

//07.將字串轉為小寫

temp=[str1 lowercasestring];

nslog(@"字串小寫:%@",temp);

//08.從字串中取前3個字元

temp=[str1 substringtoindex:3];

nslog(@"字串擷取:%@",temp);

//09.從字串中擷取第5個字元以後的字元

temp=[str1 substringfromindex:5];

nslog(@"字串擷取:%@",temp);

//10.擷取從索引5到索引13的子字元

temp=[[str1 substringfromindex:5]substringtoindex:8];

nslog(@"字串擷取:%@",temp);

temp=[str1 substringwithrange:nsmakerange(5, 8)];

nslog(@"字串擷取:%@",temp);

//11.從另一字串查詢乙個字串

nsstring *substr=@"string a";

nsrange subrange=[str1 rangeofstring:substr];

nslog(@"字串查詢:%@位於%@第:%lu個字元,長度為:%lu",substr,str1,subrange. location,subrange .length);

subrange=[str2 rangeofstring:substr];

if(subrange.location==nsnotfound)

else

}return 0;

}

輸出結果:

2014-02-12 22:41:43.890 05.字串[1560:303]字串長度:this is string a的長度為16

2014-02-12 22:41:43.892 05.字串[1560:303]字串複製:this is string a

2014-02-12 22:41:43.892 05.字串[1560:303]字串拼接:this is string athis is string b

2014-02-12 22:41:43.892 05.字串[1560:303]字串比較:this is string athis is string b不相同

2014-02-12 22:41:43.893 05.字串[1560:303]字串比較:this is string a2014-02-12 22:41:43.893 05.字串[1560:303]字串大寫:this is string a

2014-02-12 22:41:43.893 05.字串[1560:303]字串小寫:this is string a

2014-02-12 22:41:43.893 05.字串[1560:303]字串擷取:thi

2014-02-12 22:41:43.894 05.字串[1560:303]字串擷取:is string a

2014-02-12 22:41:43.894 05.字串[1560:303]字串擷取:is strin

2014-02-12 22:41:43.894 05.字串[1560:303]字串擷取:is strin

2014-02-12 22:41:43.895 05.字串[1560:303]字串查詢:string a位於this is string a:8個字元,長度為:8

2014-02-12 22:41:43.895 05.字串[1560:303]字串查詢:this is string b中不存在string a

program ended with exit code: 0

Python中的字串物件

被引號引住的內容叫字串,引號包括單引號 雙引號 三引號。capitalize 讓字串首字母大寫 lower 將字串全部轉為小寫 upper 將字串全部轉為大寫 center 數字,符號 讓字串居中 要讓數字大於字串長度 並且指定填充符號,預設空格 rjust 數字 讓字串右對齊,必須加長度 ljus...

字串物件python int 字串 字串物件

最近研究字串物件,稍微總結一下,以後繼續補充 如果我們須要把python的字串物件轉換為數整物件,我們須要用到int方法。比如 ainfo 222 print int ainfo 輸出的結果是222。然後我們檢視下ainfo在現的型別,通過type方法檢視下,發現是 而如果ainfo fefew22...

ios學習筆記之Object C 字串

在object c中,字串的宣告是 nsstring astring astring並不真正包含乙個字串物件 它是指向記憶體中字串物件的指標。和c語言中,用指標指向字串的宣告類似 nsstring a0 nsstring alloc initwithformat kevin 方法一 nsstring...