NSString的簡單使用

2021-06-28 20:50:21 字數 4326 閱讀 3886

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

//判斷兩個字串的內容是否相同,區分大小寫的

if (![string6 isequaltostring:string7])

//注意:此處在堆區建立了物件

nsstring *string8 = [[nsstring alloc] initwithformat:@"abc%@",@"def"];

nsstring *string9 = [[nsstring alloc] initwithformat:@"abc%@",@"def"];

if (string8 == string9)

if ([string8 isequaltostring:string9])

//沒有在堆區建立物件

nsstring *string10 = [[nsstring alloc] initwithstring:@"1234"];

nsstring *string11 = [[nsstring alloc] initwithstring:@"1234"];

if (string10 == string11)

//比較字串的大小compare,區分大小寫

nsstring *string12 = @"abc";

nsstring *string13 = @"abc";

nscomparisonresult result = [string12 compare:string13];

if (result == nsorderedascending) else if (result == nsorderedsame) else if (result == nsordereddescending)

//不區分大小寫比較

nscomparisonresult result1 = [string12 caseinsensitivecompare:string13];

if (result1 == nsorderedascending) else if (result1 == nsorderedsame) else if (result1 == nsordereddescending)

//------------3.字串的其他用法--------------------

nsstring *string14 = @"12345678";

//取得字串的長度

nsuinteger len = [string14 length];

nslog(@"len:%ld",len);

//轉換大小寫

nsstring *string15 = @"hello world";

//uppercasestring將字串轉換成大寫

nslog(@"upper:%@",[string15 uppercasestring]);

//lowercasestring將字串轉換成小寫

nslog(@"lower:%@",[string15 lowercasestring]);

//capitalizedstring將首字母轉換成大寫

nslog(@"capital:%@",[string15 capitalizedstring]);

//將字串轉化成基本資料型別

nsstring *string16 = @"12.3";

//錯誤

// float f = (float)string16;

// [string16 integervalue] 轉換成整型

float f1 = [string16 floatvalue];

nslog(@"f1:%f",f1);

nsstring *string17 = @"1";

bool s = [string17 boolvalue];

//字串的擷取

nsstring *string18 = @"1234567qwertyu";

//方式1:substringtoindex:從字串的開頭擷取到指定下標的地方,並且不包括指定位置的字元

nsstring *substring1 = [string18 substringtoindex:7];

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

//方式2:substringfromindex:從指定字元開始擷取到字串的結尾,並且包括指定字元

nsstring *substring2 = [string18 substringfromindex:7];

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

//方式3:substringwithrange 擷取指定的字串

nsrange range = ; //1.表示擷取的位置下標,6:擷取的長度

nsstring *substring3 = [string18 substringwithrange:range];

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

//字串的追加

nsstring *string19 = @"hello";

//在字串string19後面追加字串,返回乙個新的字串

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

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

// nsstring *string21 = [nsstring stringwithformat:<#(nsstring *), ...#>]

// nsstring *string21 = [nsstring alloc] initwithformat:<#(nsstring *), ...#>

//字串查詢

//查詢字串中是否存在@「baidu」

//如果沒有找到,location的值為nsnotfound

if (rg.location != nsnotfound)

//如果給你乙個字串:@「[email protected]」,讓你判斷是否為郵箱

//只需要查詢「@」

//通過下標取字串中的字元

nsstring *string24 = @"abcdefg";

unichar c = [string24 characteratindex:3];

nslog(@"%c",c);

/*___________________________nsmutablestring(可變字串)__________________________*/

// nsmutablestring *mutstr1 = @"abc"; 錯誤的,此處實質上是建立了乙個不可變字串

//插入

nsmutablestring *mutstring1 = [[nsmutablestring alloc] initwithstring:@"hello "];

//insertstring 在原有的字串上插入字串

[mutstring1 insertstring:@"world" atindex:6];

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

//追加

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

//刪除

nsmutablestring *mutstring2 = [[nsmutablestring alloc] initwithstring:@"可變字元符符串"];

//刪除「符符」

nsrange deletrg = [mutstring2 rangeofstring:@"符符"];

//如果找到了

if (deletrg.location != nsnotfound)

//替換

nsmutablestring *mutstring3 = [[nsmutablestring alloc] initwithstring:@"kebian字串"];

//先找到需要替換的字串

nsrange replacerg = [mutstring3 rangeofstring:@"kebian"];

//replacecharactersinrange 在原有的字串上替換字串

if (replacerg.location != nsnotfound)

return 0;

}

NSString 簡單細說

nsstring 簡單細說 一 nsstring整體架構 nsstring 簡單細說 二 nsstring的初始化 nsstring 簡單細說 三 nsstring初始化 nsstring 簡單細說 四 從url初始化 nsstring 簡單細說 五 向檔案或者url寫入 nsstring 簡單細說...

蘋果文件的使用 NSString

1.使用蘋果幫助文件 搜多nsstring,找nsstirng class reference 裡面的資訊如下 1.使用蘋果幫助文件 兩種方式 windoow documentation 搜多nsstring,找nsstirng class reference 第二種方式按住alt鍵進入 c語言是a...

OC語言 NSString的基礎使用

oc中對字串進行操作使用了foundation框架中的nsstring類 不可變 nsmutablestring類 可變 nsstring 1 建立字串 nsstring str1 我在學習oc nsstring str2 nsstring alloc initwithstring 我在學習oc n...