OC中NSString 的常用方法

2021-09-09 03:08:25 字數 3093 閱讀 2376

nsstring *str1 = @"beijing";

nsstring *str2 = @"beijing";

//全部轉為大寫

nslog(@"%@",[str1 uppercasestring]);

//全部轉為小寫

nslog(@"%@",[str1 lowercasestring]);

//首字母大寫

nslog(@"%@",[str1 capitalizedstring]);

//比較兩個字串內容是否相同

bool b =[str1 isequaltostring:str2];

//兩個字串內容比較

//nsorderedascending 右邊 > 左邊

//nsorderedsame 內容相同

//nsordereddescending 左邊 > 右邊

nscomparisonresult result = [str1 compare:str2];

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

//忽略大小寫進行比較,返回值與compare一樣

result = [str1 caseinsensitivecompare:str2];

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

//判斷字串是否以指定字串開頭

[str1 hasprefix:@"aaa"];

//判斷字串是否以指定字串結尾

[str1 hassuffix:@"aaa"];

//判斷字串是否包含指定字串,返回位置和長度

nsrange range = [@"123456" rangeofstring:@"456"];

nslog(@"%@", nsstringfromrange(range));

//反向搜尋

range = [@"123456456qweasasd456" rangeofstring:@"456" options:nsbackwardssearch];

nslog(@"%@",nsstringfromrange(range));

//指定範圍進行搜尋

range = nsmakerange(0, 9);

range = [@"123456456qweasasd456" rangeofstring:@"456" options:nsbackwardssearch range:range];

nslog(@"%@",nsstringfromrange(range));

//字串的擷取

nsstring *str = @"123456789";

nslog(@"%@",[str substringfromindex:3]);

nslog(@"%@",[str substringtoindex:6]);

nslog(@"%@",[str substringwithrange:nsmakerange(3, 3)]);

//用指定字串分割字串,返回乙個陣列

nsarray *array = [@"1,2,3,4,5,6" componentsseparatedbystring:@","];

nslog(@"%@",array);

//將陣列中的字串組合成乙個檔案路徑

nsmutablearray *components = [nsmutablearray array];

[components addobject:@"users"];

[components addobject:@"centralperk"];

[components addobject:@"desktop"];

nsstring *path = [nsstring pathwithcomponents:components];

nslog(@"%@",path); //users/centralperk/desktop

//將乙個路徑分割成乙個陣列

nsarray *array1 = [path pathcomponents];

nslog(@"%@",array1);

//判斷是否為絕對路徑(依據:是否以'/'開始)

path = @"/users/centralperk/desktop";

nslog(@"%i",[path isabsolutepath]);

//獲取最後乙個目錄

nslog(@"%@",[path lastpathcomponent]);

//刪除最後乙個目錄

nslog(@"%@",[path stringbydeletinglastpathcomponent]);

//拼接乙個目錄

//拓展名出來

//獲取拓展名,不帶.

nsstring *str2 = @"users/centralperk/desktop/test.txt";

nslog(@"%@",[str2 pathextension]);

//新增拓展名,不需要帶.

//刪除拓展名,帶.一塊刪除

nslog(@"%@",[str2 stringbydeletingpathextension]);

//字串轉為 int double float

nsstring *str3 = @"123";

nslog(@"%i",[str3 intvalue]);

nslog(@"%zi",[str3 length]);

//取出指定位置的字元

unichar c = [str3 characteratindex:2];

nslog(@"%c",c);

//轉為c語言的字串

const char *s = [str3 utf8string];

nslog(@"%s",s);

OC中NSString 的常用方法

nsstring str1 beijing nsstring str2 beijing 全部轉為大寫 nslog str1 uppercasestring 全部轉為小寫 nslog str1 lowercasestring 首字母大寫 nslog str1 capitalizedstring 比較兩...

OC中NSString 的常用方法

cpp view plain copy nsstring str1 beijing nsstring str2 beijing 全部轉為大寫 nslog str1 uppercasestring 全部轉為小寫 nslog str1 lowercasestring 首字母大寫 nslog str1 c...

OC中NSString 的常用方法

nsstring str1 beijing nsstring str2 beijing 全部轉為大寫 nslog str1 uppercasestring 全部轉為小寫 nslog str1 lowercasestring 首字母大寫 nslog str1 capitalizedstring 比較兩...