/***********************一.NSString ******************************/
/*----------------创建字符串的方法----------------*/
//1、创建常量字符串。
NSString *astring = @"Welcome to 1000phone";
//2、通过实例化方法 initWithString:实例化一个字符串对象
NSString *astring = [[NSString alloc] initWithString:@" I love iOS!"]; NSLog(@"astring:%@",astring);
[astring release];
//3、用标准c创建字符串:initWithCString:encoding:方法
const char *cString = "I love iphone";
NSString * aString = [[NSStringalloc]initWithCString:cStringencoding:NSUTF8StringEncoding];
NSLog(@"astring:%@",aString);
[aString release];
或者:用 initWithUTF8String:实例化一个字符串对象
const char *p = " Welcome to Beijing!";
NSString *string = [[NSString alloc]initWithUTF8String:p];
//4、创建格式化字符串:占位符(由一个%加一个字符组成)
int age = 23;
NSString *astring = [[NSString alloc] initWithFormat:@”I am %d”,age]]; NSLog(@"astring:%@",astring);
[astring release];
//5、通过静态方法创建字符串对象
NSString * str1 = [NSString stringWithString:@"I love programming!"];
NSString * str2 = [NSString stringWithUTF8String:" I love programming!"]; NSString * str3 = [NSString stringWithFormat:@"%@",@" I love programming!"];
//6. 从文件读取字符串:initWithContentsOfFile:
NSString *path = @" /Users/qianfeng/Desktop/StringAPI.pdf"; //文件路径 NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring);
[astring release];
/*----------------比较两个字符串----------------*/
//用 C 比较:strcmp 函数
char string1[] = "string!";
char string2[] = "string!"; if(strcmp(string1, string2) = = 0) {
NSLog(@"1"); }
//1. isEqualToString 方法 判断两个字符串是否相等 相等返回 1 不等返回 0
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result);
//2. compare 方法(compare:返回的三种值分别是-1,0,1)
//NSOrderedSame 判断两者内容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;//0NSLog(@"result:%d",result);
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring01小于 astring02为真)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; NSLog(@"result:%d",result);//-1
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring01大于 astring02为真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; NSLog(@"result:%d",result); //1
//3. 不考虑大小写比较字符串
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
enum _NSComparisonResult {
NSOrderedAscending = -1, //astring01小于 astring02NSOrderedSame, astring01 等于 astring02NSOrderedDescendingastring01大于 astring02 };
/*----------------改变字符串的大小写————————*/‘
NSString *string1 = @"A String";
NSString *string2 = @"String";
//1. uppercaseString 小写转大写 string1指向的对象内容不会发生改变 (会产生一个新的字符串对象,string指向这个新的对象)
NSString *string = [string1 uppercaseString];
NSLog(@"string:%@",string);
//2. lowercaseString 大写转小写
string = [string1 lowercaseString]; NSLog(@"string:%@",string);
//3. capitalizedString 单词首字母大写 其余小写,其他字符保持不变
NSLog(@"string:%@",string);//首字母大小
/*----------------搜索子串----------------*/
NSString *string1 = @"I love iOS very much!";
NSString *string2 = @"iOS";
NSRange range = [string1 rangeOfString:string2]; //NSRange 代表一个范围:子串 string2在长串 string1中的范围(位置和长度)
NSUInteger location=range.location;
NSUInteger length= range.length;
NSString *astring = [NSString stringWithFormat:@"Location:%lu,Leight:%lu",location, length]; NSLog(@"astring:%@",astring);
[astring release];
/*----------------抽取子串 ----------------*/
//1. -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3]; NSLog(@"string2:%@",string2);
//2. -substringFromIndex: 从指定位置开始(包括指定位置的字符)提取,并包括之后的全部字符直到最后;
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3]; NSLog(@"string2:%@",string2);
//3. -substringWithRange: //在指定范围内从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string2:%@",string2);
/*------------判断字符串内是否还包含别的字符串(前缀,后缀)------------*/
//01:检查字符串是否以另一个字符串开头
- (BOOL) hasPrefix: (NSString *) aString; (判断前缀)
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
//02:检查字符串是否以另一个字符串结尾
- (BOOL) hasSuffix: (NSString *) aString; (判断后缀)
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
字符串转化
NSString * str = @"hello"; const char *p = [str UTF8String];将OC字符串转化为C的字符串 1、切割字符串 NSArray *array = [content componentsSeparatedByString:@“\n”]; 2、获取文件的拓展名 NSString * ewer = [path pathExtension]; 3、//比较两个字符串是否相等。 NSLog(@"%d",[str1 isEqualToString:str2]); isEqualToClass 4、//字符串比较大小 NSComparisonResult compare = [str1 compare:str2]; if (compare == NSOrderedAscending) { NSLog(@"左边的小"); }elseif (compare == NSOrderedSame){ NSLog(@"两边相等"); }elseif (compare == NSOrderedDescending){ NSLog(@"左边的大"); }
5、 //不区分大小写字符串比较大小 NSComparisonResult cmp = [str1 caseInsensitiveCompare:str2]; if (cmp == NSOrderedAscending) { NSLog(@"左边的小"); }elseif (cmp == NSOrderedSame){ NSLog(@"两边相等"); }elseif (cmp == NSOrderedDescending){ NSLog(@"左边的大"); 6、 //判断前缀是否是www NSLog(@"%d",[str03hasPrefix:@"1www"]);
//判断后缀是否是cn NSLog(@"%d",[str03 hasSuffix:@"c n"]); integervalue 是字符串里的数字转换成长整型、 //把OC字符串里面的数字字符,转换为int类型的数据. intValue第一个非数字字符的时候停止转换。 整形的所有小数点都是非数字子符。 //把OC字符串里面的数字字符,转换为double类型的数据。doubleValue //把OC字符串里面的数字字符,转换为float类型的数据。floatValue //把OC字符串里面的数字字符,转换为longlong类型的数据。longlongValue 8、查询字符串的子串 NSMutableString *str = [NSMutableStringstringWithFormat:@"When I was young, I loved a girl in neighbor class."]; NSMutableString *str1 = [NSMutableStringstringWithFormat:@"a teacher, I rather to teach student"]; NSMutableString *str2 = [NSMutableStringstringWithFormat:@"young, I loved a girl"]; NSRange range=[strrangeOfString:str2];如果找不到 length = 0,location= nsnotfound(920…..) //替换用一个新的子串 替换指定范围内容字符串 [str replaceCharactersInRange:(range)withString:str1]; #define SHOW(obj) NSLog(@"起始位置%ld,长度:%ld",obj.location,obj.length) |
(OC 的字符 :typedef unsigned short unichar)
NSLog(@"%ld",[str1 length]); //求字符串长度
NSLog(@"%c", [str1 characterAtIndex:1]);//获取字符串中的字符(UNIchar 可以接收 )
BOOL ret = [str1 isEqualTo:str2];//比较两个字符串是否相等,相等返回YES(1),不相等返回NO(0)(BOOL YES(1) NO(0))
long ret1 = [str1 compare:str2];//比较两个字符串的大小,str1大于返回1 相等返回0 小于返回-1
long ret2 = [str1 caseInsensitiveCompare:str2];//不区分大小写比较字符串大小
NSString * ptr = [str2 uppercaseString];//将字符串中的所有小写字符转换成大写不改变原来的字符串
ptr = [str1 lowercaseString];//将字符串中的所有大写字符转换成小写不改变原来的字符串
ptr = [str3 capitalizedString];//将字符串中出现的第一个字母转换成大写,其余字母小写
NSString * str4 = @"hello world";
NSRange range = [str4 rangeOfString:@"wr"];//查找子串,找不到返回NSNotFound找到返回location和length
if (range.location != NSNotFound) {
NSLog(@"%ld %ld",range.location, range.length);
}
NSString * str5 = @"helloworld";
NSString * ptr1 = [str5 substringToIndex:4];//字符串抽取从下标0开始到4不包括4
NSString * ptr2 = [str5 substringFromIndex:5];//从下标5开始抽取到字符串结束包括5
NSRange range1 = {4,3};//结构体初始化
NSString * ptr3 = [str5 substringWithRange:range1];//在range指定范围内抽取
NSString * ptr4 = [str5 substringWithRange:NSMakeRange(4,3)];//NSMakeRange可以生成一个结构体
NSString * str = @"www.1000phone.com";
BOOL ret = [str hasPrefix:@"www"];//判断字符串是否以指定字符串开头
NSString * str1 = @"1.txt";
BOOL ret1 = [str hasSuffix:@".txt"];//判断字符串是否以指定的字符串结尾
NSMutableString可变字符串(动态增加和减少的) 继承于NSString可以使用NSString的所有方法
NSMutableString * str = [[NSMutableString alloc]initWithString:@"hello"];//将不可变的字符串转换为可变的字符串
[str insertString:@"123" atIndex:1];//在指定下标(不要越界)位置插入NSString类型字符串
[str appendString:@"123"];//在字符串末尾追加字符串
[str deleteCharactersInRange:NSMakeRange(0, 2)];//从指定下标删除length个字符
[str setString:@"qianfeng"];//修改字符串亦称对该可变字符串赋值
[str replaceCharactersInRange:NSMakeRange(3, 1) withString:@"ios"];//将指定下标位置的length个字符替换为指定的字符串