iOS Cookbook1 Objective-C 字符串
来源:互联网 发布:java软件开发技术 编辑:程序博客网 时间:2024/06/11 02:39
从网上搜集汇编的。
1、创建常量字符串。
NSString *astring = @"This is a String!";
2、创建空字符串,给予赋值。
NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; NSLog(@"astring:%@",astring); [astring release];
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); [astring release];
4、用标准c创建字符串:initWithCString方法
char *Cstring = "This is a String!"; NSString *astring = [[NSString alloc] initWithCString:Cstring]; NSLog(@"astring:%@",astring); [astring release];
5、创建格式化字符串:占位符(由一个%加一个字符组成)
int i = 1; int j = 2; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]]; NSLog(@"astring:%@",astring); [astring release];
6、创建临时字符串
NSString *astring; astring = [NSString stringWithCString:"This is a temporary string"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring); [astring release]; NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES]; [astring release];
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 = [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为真)
8、如何判断字符串为空
NSString *urlString = [urlInput stringValue];if (!urlString) { NSLog( @”NO INPUT.” );} else { if ([urlString length] == 0 ) { NSLog( @”NO INPUT.” ); } else { } }
9、字符串的替换
注:将字符串中的参数进行替换
参数1:目标替换值
参数2:替换成为的值
参数3:类型为默认:NSLiteralSearch
参数4:替换的范围
[str replaceOccurrencesOfString:@"1" withString:@"222" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
10、给字符串分配容量
NSMutableString *String; String = [NSMutableString stringWithCapacity:40];
11、追加字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
12、在已有字符串中按照所给出范围和长度删除字符
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 deleteCharactersInRange:NSMakeRange(0, 5)];
13、在已有字符串后面在所指定的位置中插入给出的字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];[String1 insertString:@"Hi! " atIndex:0];
14、按照所给出的范围,和字符串替换的原有的字符
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
15、判断字符串内是否还包含别的字符串(前缀,后缀)
NSString *String1 = @"NSStringInformation.txt";[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
16、返回一个数组,包含从已经由一个给定的分隔符分为接收器串。
- (NSArray*)componentsSeparatedByString:(NSString*)NString
参数
分离器
分隔符的字符串。
NSString *list = @"Norman, Stanley, Fletcher";NSArray *listItems = [list componentsSeparatedByString:@", "];listItems包含了:
{ @"Norman", @"Stanley", @"Fletcher" }.
17、是否包含该字符串
NSRange range = [@"字符串--A" rangeOfString:“是否包含--B”];if (range.location == NSNotFound){//不包含}else{//包含}
- iOS Cookbook1 Objective-C 字符串
- ios objective c 字符串各种处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS开发之路-Objective-C 字符串
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- IOS基础:Objective-C 字符串处理
- iOS Objective—C 字符串应用
- iOS/Objective-C字符串NSString操作总结20130608更新
- ios objective-c之-6:字符串NSString
- iOS Objective c 16进制字符串转为二进制数组
- Objective-c 字符串遍历
- objective-c 字符串格式化
- Objective-C字符串处理
- AST in JDT
- 算法题目练习
- 最长公共上升子序列(LCIS)的O(n^2)算法
- C语言中的位运算和逻辑运算
- Eclipse更改默认字符集 设置UTF-8
- iOS Cookbook1 Objective-C 字符串
- 腾讯马拉松-为什么时光不能倒流-hdu4510
- hdu 3068(扩展KMP)
- 黑马程序员 面试技巧<二>--->交通灯管理系统
- 浅谈windows句柄表
- EOJ 2855 贪吃蛇 [2011年研究生机试 热身]
- 每日阅读4之linux内核设计与实现——linux调度实现之调度器入口
- 一位前辈工程师的忠告
- Android Fragments 详细使用