1、字符转为字符串
//OC的NSString 类提供了根据c语言字符串创建NSString对象的初始化方法和便利构造器
char *cString = "fendoudexiaolaoshu";
NSString *string = [[NSString alloc] initWithCString:cString
encoding:NSUTF8StringEncoding];
//C语言字符串在转成OC字符串对象时,需要进行转码,转码格式选择NSUTF8StringEncoding枚举值。
NSString *string = [NSString stringWithCString:cString
encoding:NSUTF8StringEncoding];
2.根据文档内容创建oc字符串对象
/**
文件路径
*/
NSString *string = [[NSString alloc]initWithContentsOfFile:@"/Users/xalo/Desktop/622848.txt" encoding:NSUTF8StringEncoding error:nil];
3.获取指定下标的字符
//characterAtIndex:方法可以获取方法调用的字符串对象指定下标的字符,下标从0到length-1
char character = [string8 characterAtIndex:i];
4.比较两个字符串是否相同
BOOL result = [string1 isEqualToString:string2 ];
NSLog(@"%@",result ? @"YES" : @"NO");
5.比较两个字符串对象的大小顺序
//比较两个字符串对象的大小顺序,-1表示调用方法的字符串比参数字符串小,为升序,0 表示一致,1表示调用方法的字符串比较参数字符串大,为降序。
NSComparisonResult compareResult = [string9 compare:string10];
6.获取从指定下标开始(包含指定下标)到字符串结尾的子字符串
NSString *subString1 = [string8 substringFromIndex:2];
7.获取从原始字符串起始下标开始到指定下标(不包含指定下标)的子字符串
NSString *subString2 = [string8 substringToIndex:3];
8.结构体NSRange包含两个长整形成员,分别为起始下标和长度。如果使用range截取字符串,则表示从指定下标开始截取指定长度的子字符串并返回
NSRange range = NSMakeRange(1, 2);
NSString *subString3 = [string8 substringWithRange:range];
9.字符串拼接的两种方法
//字符串拼接的两种方法
NSString *appendString1 = [string10 stringByAppendingString:@" hahahah"];
NSLog(@"appendString1==%@",appendString1);
//拼接指定格式的字符串
NSString *appendString2 = [string10 stringByAppendingFormat:@" %d %c %d",3,'+',5];
NSLog(@"appendString2===%@",appendString2);
NSString *appendString3 = [@"3 + 5 " stringByAppendingFormat:@"%@",string10];
NSLog(@"%@",appendString3);
10.通过指定的参数字符串替换对应范围的字符们
NSString *replacedString = [string8 stringByReplacingCharactersInRange:NSMakeRange(1, 3) withString:@"haha"];
NSLog(@"%@",replacedString);
NSString *textString = @"abcdefghijklmn";
NSRange replaceRange = [textString rangeOfString:@"efg"];//获取指定字符串在原字符串的范围
NSString *resultStr = [textString stringByReplacingCharactersInRange:replaceRange withString:@"abc"];
NSLog(@"%@",resultStr);
11.判断字符串是否包含指定字符串
BOOL result1 = [string8 containsString:@"am"];
NSLog(@"%@",result1 ? @"YES" : @"NO");
12.返回大小写字符串
//返回大写字符串
NSLog(@"%@",string8.uppercaseString);//大写字符串
//返回小写字符串
NSLog(@"%@",string8.lowercaseString);//小写字符串
//返回首字母大写字符串
NSLog(@"%@",string8.capitalizedString);//首字母大写
13.判断指定字符串是否有前缀字符串或后缀字符串
//判断指定字符串是否有前缀字符串
BOOL result2 = [string8 hasPrefix:@"http"];
NSLog(@"%@",result2 ? @"YES" : @"NO");
//判断指定字符串是否有后缀字符串
BOOL result3 = [string8 hasSuffix:@".com"];
NSLog(@"%@",result3 ? @"YES" : @"NO");
14.可变字符串
//根据不可变字符串对象创建同内容的可变字符串对象
NSMutableString *mutableStr1 = [[NSMutableString alloc] initWithString:@"lanou3g"];
NSLog(@"%@",mutableStr1);
NSMutableString *mutableStr2 = [NSMutableString string];
NSLog(@"mutableStr2:%@",mutableStr2);
//对于可变字符串,可以在创建对象后,调用设置字符串的方法。
[mutableStr2 setString:@"lanou3g"];
NSLog(@"%@",mutableStr2);
//可变字符串的两种拼接方法,除了可以使用继承自NSString类的拼接方法返回结果字符串,可变字符串提供直接拼接修改字符串对象的方法。
[mutableStr2 appendString:@" haha"];
NSLog(@"%@",mutableStr2);
[mutableStr2 appendFormat:@" %d %c %d",3,'+',5];
NSLog(@"%@",mutableStr2);
//为可变字符串在指定下标插入字符串的方法。
[mutableStr2 insertString:@"beijing" atIndex:7];
NSLog(@"%@",mutableStr2);
//通过继承来的方法获取指定字符串在可变字符串的范围
NSRange deleteRange = [mutableStr2 rangeOfString:@"beijing"];
//为可变字符串删除指定范围的字符串
[mutableStr2 deleteCharactersInRange:deleteRange];
NSLog(@"%@",mutableStr2);
//可变字符串提供的用于替换指定范围的字符为给定的字符串的方法。
[mutableStr2 replaceCharactersInRange:NSMakeRange(1, 4) withString:@"hahah"];
NSLog(@"%@",mutableStr2);
15.基本类型的数据创建NSNumber类型的对象
//int
NSNumber *intNumber = [NSNumber numberWithInt:20];
//float
NSNumber *floatNumber = [NSNumber numberWithFloat:3.14];
//long
NSNumber *longNumber = [NSNumber numberWithLong:45698215];
//char
NSNumber *charNumber = [NSNumber numberWithChar:'a'];
//shout
NSNumber *shortNumber = [NSNumber numberWithShort:154];
//double
NSNumber *doubleNumber = [NSNumber numberWithDouble:456545481215.6523];
//bool
NSNumber *boolNumber = [NSNumber numberWithBool:YES];
//integer
NSNumber *integerNumber = [NSNumber numberWithInteger:456];
//longlong
NSNumber *longLongNumber = [NSNumber numberWithLongLong:464544454584454645];
16.将已有的NSNumber类型的对象转化成基本类型的数据,使用NSNumber类提供的相关属性
NSLog(@"%d",intNumber.intValue);
NSLog(@"%f",floatNumber.floatValue);
NSLog(@"%f",doubleNumber.doubleValue);
NSLog(@"%c",charNumber.charValue);
NSLog(@"%ld",longNumber.longValue);
NSLog(@"%lld",longLongNumber.longLongValue);
NSLog(@"%d",shortNumber.shortValue);
17.比较两个基本类型的大小
NSComparisonResult compareResult1 = [intNumber compare:floatNumber];
NSLog(@"%ld",compareResult1);
NSComparisonResult compareResult2 = [charNumber compare:doubleNumber];
NSLog(@"%ld",compareResult2);