您好,登錄后才能下訂單哦!
/*******************************************************************************************
NSString
*******************************************************************************************/
//一、NSString
/*----------------創建字符串的方法----------------*/
//1、創建常量字符串。
NSString*astring = @"This is a String!";
//2、創建空字符串,給予賦值。
NSString*astring =[[NSStringalloc]init];
astring = @"This is a String!";
[astring release];
NSLog(@"astring:%@",astring);
//3、在以上方法中,提升速度:initWithString方法
NSString*astring =[[NSStringalloc]initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
[astring release];
//4、用標準c創建字符串:initWithCString方法
char*Cstring="This is a String!";
NSString*astring =[[NSStringalloc]initWithCString:Cstring];
NSLog(@"astring:%@",astring);
[astring release];
//5、創建格式化字符串:占位符(由一個%加一個字符組成)
int i =1;
int j =2;
NSString*astring =[[NSStringalloc]initWithString:[NSStringstringWithFormat:@"%d.This is %i string!",i,j]];
NSLog(@"astring:%@",astring);
[astring release];
//6、創建臨時字符串
NSString*astring;
astring =[NSStringstringWithCString:"This is a temporary string"];
NSLog(@"astring:%@",astring);
/*----------------從文件讀取字符串:initWithContentsOfFile方法----------------*/
NSString*path= @"astring.text";
NSString*astring =[[NSStringalloc]initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
/*----------------寫字符串到文件:writeToFile方法----------------*/
NSString*astring =[[NSStringalloc]initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString*path= @"astring.text";
[astring writeToFile:pathatomically:YES];
[astring release];
/*----------------比較兩個字符串----------------*/
//用C比較:strcmp函數
char string1[]="string!";
char string2[]="string!";
if(strcmp(string1, string2)==0)
{
NSLog(@"1");
}
//isEqualToString方法
NSString*astring01 = @"This is a String!";
NSString*astring02 = @"This is a String!";
BOOL result =[astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
//compare方法(comparer返回的三種值)
NSString*astring01 = @"This is a String!";
NSString*astring02 = @"This is a String!";
BOOL result =[astring01 compare:astring02]== NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedSame判斷兩者內容是否相同
NSString*astring01 = @"This is a String!";
NSString*astring02 = @"this is a String!";
BOOL result =[astring01 compare:astring02]== NSOrderedAscending;
NSLog(@"result:%d",result);
//NSOrderedAscending判斷兩對象值的大小(按字母順序進行比較,astring02大于astring01為真)
NSString*astring01 = @"this is a String!";
NSString*astring02 = @"This is a String!";
BOOL result =[astring01 compare:astring02]== NSOrderedDescending;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小于astring01為真)
//不考慮大小寫比較字符串1
NSString*astring01 = @"this is a String!";
NSString*astring02 = @"This is a String!";
BOOL result =[astring01 caseInsensitiveCompare:astring02]== NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小于astring01為真)
//不考慮大小寫比較字符串2
NSString*astring01 = @"this is a String!";
NSString*astring02 = @"This is a String!";
BOOL result =[astring01 compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch]== NSOrderedSame;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。