//建立乙個字串物件
nsstring *str1 = @"www.hello.txt";
nslog(@"%@",str1);
//用格式化字串初始化
int a = 123;
nsstring *str2 =[[nsstring alloc]initwithformat:@"%d %@",a,str1];
nslog(@"%@",str2);
//快速建立乙個字串常量
nsstring *string1 = @"abc";
nsstring *string2 = @"cde";
nslog(@"%@ %@",string1,string2);
//建立乙個格式化的字串 (內存在堆區)
nsstring *string3 = [[nsstring alloc]initwithformat:@"整數: %d",10];
nsstring *string4 = [[nsstring alloc]initwithformat:@"浮點數: %f",10.0];
nslog(@"%@ %@",string3,string4);
//字串比較
nsstring *string5 = [nsstring stringwithformat:@"123"];
nsstring *string6 = [nsstring stringwithformat:@"124"];
//測試內容是否相等
//方法一
if ([string5 isequaltostring:string6]) else
//方法二
//bool : yes(1) no(0)
bool rst = [string5 compare:string6];
nslog(@"%d",rst);
//方法三
//不區分大小寫比較字串大小
nsstring *astring = @"abc";
nsstring *otherstring = @"abc";
nscomparisonresult rst3 = [astring compare:otherstring];
nslog(@"%ld",rst3);
//測試內容是否指向同乙個物件
if (string5 == string6) else
//求字串長度
nslog(@"%ld",[string6 length]);
//擷取字串中的字元
nslog(@"%c",[string6 characteratindex:2]);
//字串轉換
nsstring *string7 = @"hello world";
nslog(@"%@",[string7 uppercasestring]);
nslog(@"%@",[string7 lowercasestring]);
nslog(@"%@",[string7 capitalizedstring]);
//轉換成基本資料型別
nsstring *string8 = @"3.14";
float pi = [string8 floatvalue];
nslog(@"%f",pi);
//將oc字元轉化為c的字元
const char *p = [string8 utf8string];
nslog(@"%c",*p);
//將字串轉換為陣列
nsstring *string9 = @"abc bcd xyz";
nsarray *array = [string9 componentsseparatedbystring:@" "];
nslog(@"array: %@",array);
//擷取字串
nsstring *string10 = @"abcdefg";
nsstring *substring1 = [string10 substringtoindex:2];
nslog(@"%@",substring1);
nsstring *substring2 = [string10 substringfromindex:2];
nslog(@"%@",substring2);
//通過查詢擷取
nsrange range = ;
range.location = 4;
range.length = 2;
//在range指定範圍內抽取
nsstring *substring3 = [string10 substringwithrange:range];
nslog(@"%@",substring3);
//nsmakerange
nsstring *substring4 = [string10 substringwithrange:nsmakerange(4, 3)];
nslog(@"%@",substring4);
//字串拼接
nsstring *string11 = @"abc";
nsstring *string12 = @"xyz";
//查詢字串物件
nsstring *link = @"abcdetarget=_blankxyz";
//查詢子串,找不到返回
nsrange range1 = [link rangeofstring:@"target=_blank"];
if (range1.location != nsnotfound)
//判斷字串是否以指定字串開頭
bool rst5 = [string1 hasprefix:@"www"];
nslog(@"%d",rst5);
//判斷字串是否以指定字串結尾
bool rst6 = [string1 hassuffix:@".txt"];
nslog(@"%d",rst6);
//可變字串物件:nsmutablestring繼承至string
nsmutablestring *mutablestring1 = [[nsmutablestring alloc]initwithformat:@"abc"];
nslog(@"%@",mutablestring1);
//對可變字串賦值
[mutablestring1 setstring:@"qianfeng"];
nslog(@"%@",mutablestring1);
//插入乙個字串,在指定下標(不要越界)位置插入nsstring型別字元
[mutablestring1 insertstring:@"...xyz" atindex:3];
nslog(@"%@",mutablestring1);
//在字串末尾追加字串
nslog(@"%@",mutablestring1);
//替換
nsrange range2=;
[mutablestring1 replacecharactersinrange:range2 withstring:@"efg"];
nslog(@"%@",mutablestring1);
//刪除
[mutablestring1 deletecharactersinrange:range2];
nslog(@"%@",mutablestring1);
NSString進行比較的方法和屬性
pragma mark string comparison and equality 這是字串用來比較的方法 nscomparisonresult compare nsstring string nscomparisonresult compare nsstring string options n...
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 建立乙個新字串並將其設定為...