温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Objective-C常用代码

发布时间:2020-08-01 11:38:46 来源:网络 阅读:235 作者:againbike1 栏目:开发技术

/*******************************************************************************************
    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;



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI