《ios开发经常用到的代码》由会员分享,可在线阅读,更多相关《ios开发经常用到的代码(14页珍藏版)》请在金锄头文库上搜索。
1、文档供参考,可复制、编制,期待您的好评与关注! #include/* 说明 malloc, NULL, size_t */#include/* 说明 va_ 相关类型和函数 */#include/* 说明 strcat 等 */char *vstrcat(const char *first, .)size_t len;char *retbuf;va_list argp;char *p;if(first = NULL)return NULL;len = strlen(first);va_start(argp, first);while(p = va_arg(argp, char *) != NU
2、LL)len += strlen(p);va_end(argp);retbuf = malloc(len + 1); /* +1 包含终止符 0 */if(retbuf = NULL)return NULL; /* 出错 */(void)strcpy(retbuf, first);va_start(argp, first); /* 重新开始扫描 */while(p = va_arg(argp, char *) != NULL)(void)strcat(retbuf, p);va_end(argp);retbuf = malloc(len + 1); /* +1 包含终止符 0 */if(ret
3、buf = NULL)return NULL; /* 出错 */(void)strcpy(retbuf, first);va_start(argp, first); /* 重新开始扫描 */while(p = va_arg(argp, char *) != NULL)(void)strcat(retbuf, p);va_end(argp);return retbuf;%c 一个单一的字符%d 一个十进制整数%i 一个整数%e, %f, %g 一个浮点数%o 一个八进制数%s 一个字符串%x 一个十六进制数%p 一个指针%n 一个等于读取字符数量的整数%u 一个无符号整数% 一个字符集% 一个精
4、度符号 /一、NSString /*-创建字符串的方法-*/ 1、创建常量字符串。 NSString *astring = This is a String!; 2、创建空字符串,给予赋值。 NSString *astring = init; astring = This is a String!; NSLog(astring:%,astring); ; 3、在以上方法中,提升速度:initWithString方法 NSString *astring = initWithString:This is a String!; NSLog(astring:%,astring); ; 4、用标准c创建
5、字符串:initWithCString方法 char *Cstring = This is a String!; NSString *astring = initWithCString:Cstring; NSLog(astring:%,astring); ; 5、创建格式化字符串:占位符(由一个%加一个字符组成) int i = 1; int j = 2; NSString *astring = initWithString:; NSLog(astring:%,astring); ; 6、创建临时字符串 NSString *astring; astring = ; NSLog(astring:
6、%,astring); /*-从文件读取字符串:initWithContentsOfFile方法 -*/ NSString *path = astring.text; NSString *astring = initWithContentsOfFile:path; NSLog(astring:%,astring); ; /*-写字符串到文件:writeToFile方法 -*/ NSString *astring = initWithString:This is a String!; NSLog(astring:%,astring); NSString *path = astring.text;
7、 ; ; /*- 比较两个字符串-*/ 用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 = ; NSLog(result:%d,result); compare方法(comparer返回的三种值) NSStri
8、ng *astring01 = This is a String!; NSString *astring02 = This is a String!; BOOL result = = = NSOrderedSame; NSLog(result:%d,result); NSOrderedSame 判断两者内容是否相同 NSString *astring01 = This is a String!; NSString *astring02 = this is a String!; BOOL result = = = NSOrderedAscending; NSLog(result:%d,resul
9、t); /NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) NSString *astring01 = this is a String!; NSString *astring02 = This is a String!; BOOL result = = = NSOrderedDescending; NSLog(result:%d,result); /NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) 不考虑大 小写比较字符串1 NSStrin
10、g *astring01 = this is a String!; NSString *astring02 = This is a String!; BOOL result = = = NSOrderedSame; NSLog(result:%d,result); /NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为 真) 不考虑大小写比较字符串2 NSString *astring01 = this is a String!; NSString *astring02 = This is a String!; BOOL re
11、sult = astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch = = NSOrderedSame; NSLog(result:%d,result); /NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 /*-改变字符串的大小写-*/ NSString *string1 = A String; NSString *string2 = String;
12、 NSLog(string1:%,);/大写 NSLog(string2:%,);/小写 NSLog(string2:%,);/首字母大小 /*-在串中搜索子串 -*/ NSString *string1 = This is a string; NSString *string2 = string; NSRange range = ; int location = range.location; int leight = range.length; NSString *astring = initWithString:NSString stringWithFormat:Location:%i,Leight:%i ,location,leight; NSLog(astring:%,astring); ; /*-抽取子串 -*/ -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符 NSString *string1 = This is a string; NSString *string2 = ; NSLog(string2:%,string2); -substringFromIndex: