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);
NSString常用方法
02 id string 建立乙個新的字串 03 id stringwithstring nsstring 建立乙個新字串,並將其設定為nsstring變數值 04 nsstring stringwithformat format,arg,arg1,arg2.格式化乙個字串到變數 05 id id ...
NSString 常用方法
建立乙個新字串並將其設定為 path 指定的檔案的內容,使用字元編碼enc,在error上返回錯誤 id stringwithcontentsofurl nsurl url encoding nsstringencoding enc error nserror error 建立乙個新字串並將其設定為...
NSString的常用方法
nsstring類用於處理字串物件,以 開頭,引號當中則是字串的內容,一旦建立就不可以修改。1.字串的建立 nsstring string 建立乙個字串常量 建立乙個字串常量 nsstring string nsstring alloc init 建立乙個空的字串 nsstring string n...