OC学习4
可变字符串学习
/*
开学 OC 中 Foundation 框架下的基础类
*/
/* NSValue和NSNumber
功能
将 OC 和 C 语言当中的基本数据类型转换成实例对象,即将值类型转换成引用类型
NSValue 为NSNumber的父类
其中NSValue类可以将CGPoint,CGSize,CGRect,NSRange,NSEdgeInsets转换成对象
其中NSNumber类可以将int,float,double,char,bool等 C 中的基本数据类型转换对象
我们将 C 和 OC 中的基本数据类型(值类型)转换成对象(引用类型)的过程,叫做封装.相对应的,也有一个将对象转变成基本数据类型,此过程叫做拆包.
字符串操作:
1.字符串长度
2.字符串的截取
3.字符串比较
"=="表示两个字符串的指针和内容都相同
"xxx isEqualToString:yyy"表示两个字符串内容是否相同
4.根据字符串拿到对应的range
5.判断字符串中是否有XX前缀或XX后缀
6.字符串拼接
note:字符串操作的代码实现在下面.
*/
//以下为基本数据类型
int a = 5 ;
float b = 4.5 ;
double c = 3.14 ;
char d = 'c' ;
bool flag = YES ;
//以上注释的3种,通常不以这种方式来编写
CGPoint point = CGPointMake(3 , 4 );
CGSize size = CGSizeMake(30 , 47 );
CGRect rect = CGRectMake(1 , 2 , 3 , 4 );
NSRange range = NSMakeRange(3 , 7 );
//转换
NSValue *pointValue = [NSValue valueWithPoint:point];
NSValue *sizeValue = [NSValue valueWithSize:size];
NSValue *rectValue = [NSValue valueWithRect:rect];
NSValue *rangeValue = [NSValue valueWithRange:range];
NSNumber *intNumber = [NSNumber numberWithInt:1 ];
NSNumber *integerNumber = [NSNumber numberWithInteger:12 ];
NSNumber *floatNumber = [NSNumber numberWithFloat:12.3 ];
NSNumber *doubleNumber = [NSNumber numberWithDouble:12.45 ];
NSNumber *charNumber = [NSNumber numberWithChar:'b' ];
NSNumber *boolNumber = [NSNumber numberWithBool:YES ];
//拆包
CGPoint point1 = [pointValue pointValue];
CGSize size1 = [sizeValue sizeValue];
CGRect rect1 = [rectValue rectValue];
NSRange range1 = [rangeValue rangeValue];
int a1 = [intNumber intValue];
NSInteger a2 = [integerNumber integerValue];
float a3 = [floatNumber floatValue];
double a4 = [doubleNumber doubleValue];
char a5 = [charNumber charValue];
bool a6 = [boolNumber boolValue];
//NSString 和 NSMutableString
/*OC中的字符串具有强大的功能,即封装性极强,我们只需要找到对应API(API指的是功能,方法),就可以对字符串做相应的操作.OC中字符串分为不可亦字符串和可变字符串,其中可变字符串是不可变字符串的子类.
在ios开发中,字符串通常用作显示文本,即作为UILable,UITextFeild等一些UIKit框架下控件的显示文本
*/
/*
NSString 不可变字符串
1.快速初始化
NSString *string1 = @"bokanwisdom" ;
2.格式化的方式初始化
NSString *string4 = [NSString stringWithFormat:@"%d" ,3 ];
note:对于用格式化方式来构造字符串实例来讲,其作用并不仅仅来构造一个字符串对象,它还可以用来做字符串拼接
3.从本地文件中读取字符串
NSString *string8 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error :&error ];
NSString *string9 = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error :&error ];
4.从网络中读取一个字符串
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com" ];
NSString *string10 = [NSString stringWithContentsOfURL:url encoding:4 error:&error];
//1.初始化一个字符串对象
NSString *string1 = @"bokanwisdom" ;
NSString *string2 = [NSString stringWithString:@"bokanwisdom" ];
NSString *string3 = [[NSString alloc] initWithString:@"bokanwisdom" ];
NSString *string4 = [NSString stringWithFormat:@"%d" ,3 ];
NSString *string5 = [NSString stringWithFormat:@"%dbokanwisdom" ,3 ];
NSLog (@"string4 = %@" ,string5);
//对于用格式化方式来构造字符串实例来讲,其作用并不仅仅来构造一个字符串对象,它还可以用来做字符串拼接
NSString *string6 = @"XXX" ;
NSString *string7 = @"bbb" ;
NSString *newString = [NSString stringWithFormat:@"%@%@" ,string6,string7];
NSLog (@"%@" ,newString);
//在本地文件中读取字符串,并且知道该文件的编码方式
NSString *path = @"/Users/4001/Desktop/test.html" ;
NSError *error;
NSString *string8 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSLog (@"%@" ,string8);
//在本地文件中读取字符串,但不知道该文件的编码方式
NSStringEncoding encoding;
NSString *string9 = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:&error];
NSLog (@"%@,%lu" ,string9,(unsigned long )encoding);
//从网络文件中读取字符串
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com" ];
NSString *string10 = [NSString stringWithContentsOfURL:url encoding:4 error:&error];
NSLog (@"string10 = %@" ,string10);
//字符串的操作
NSString *string11 = @"当女朋友说“随便”的时候,她的意思其实是:老娘懒得去想,也想不出好的,虽然是让你看着办,但你一定要想出我满意的才行" ;
//字符串截取
NSString *subStr = [string11 substringToIndex:8 ];
NSLog (@"subStr = %@" ,subStr);
NSString *subStr1 = [string11 substringFromIndex:8 ];
NSLog (@"subStr1 = %@" ,subStr1);
NSString *subStr2 = [string11 substringWithRange:NSMakeRange(8 , (string11.length ) - 8 )];
NSLog (@"subStr2 = %@" ,subStr2);
//字符串的属性 长度 length
//字符串比较 如何判断两个字符串是否相等?
if (subStr2 == subStr1) {
NSLog (@"subStr1 和 subStr2 相等" );
}else {
NSLog (@"subStr1 和 subStr2 不相等" );
}
//在实际开发过程中,我们通常只需要比较两个字符串的内容是否相等
if ([subStr1 isEqualToString:subStr2]) {
NSLog (@"subStr1 和 subStr2 相等" );
}else {
NSLog (@"subStr1 和 subStr2 不相等" );
}
//比较字符串大小
NSString *string12 = @"yanshuxin" ;
NSString *string13 = @"Hello" ;
NSComparisonResult result = [string12 compare:string13];
switch (result) {
case NSOrderedAscending:
NSLog (@"string12 < string13" );
break ;
case NSOrderedSame:
NSLog (@"string12 = string13" );
break ;
case NSOrderedDescending:
NSLog (@"string12 > string13" );
break ;
default :
break ;
}
//根据字符串拿到对应的range
NSRange strRange = [string11 rangeOfString:@"你看" ];
NSLog (@"strRange = %@" ,[NSValue valueWithRange:strRange]);
NSRange strRange1 = [string11 rangeOfString:@"你" ];
NSLog (@"strRange1 = %@" ,[NSValue valueWithRange:strRange1]);
//判断字符串中是否有XX前缀或XX后缀
NSString *string14 = @"www.baidu.com" ;
if ([string14 hasPrefix:@"http://" ]) {
NSLog (@"含有此前缀" );
}else {
NSLog (@"无此前缀" );
}
NSString *string15 = @"asdf.jpg" ;
if ([string15 hasSuffix:@".jpg" ] || [string15 hasSuffix:@".png" ]) {
NSLog (@"含有此后缀" );
}
//字符串拼接
NSString *connStr = [@"http://" stringByAppendingString:string14];
NSLog (@"connStr = %@" ,connStr);
NSString *domainStr = @"http://wechat.palm-h.com" ;
NSString *xxx = @"login" ;
NSString *string16 = [NSString stringWithFormat:@"%@/%@" ,domainStr,xxx];
NSLog (@"string16 = %@" ,string16);
NSString *string17 = [domainStr stringByAppendingPathComponent:xxx];
NSLog (@"string17 = %@" ,string17);
NSString *imgPath = @"http://web.52pk.com/uploads/141017/30_105806_1_lit.jpg" ;
imgPath = [imgPath lastPathComponent];
NSLog (@"imgPath = %@" ,imgPath);
path = [path lastPathComponent];
NSLog (@"path = %@" ,path);
NSString *theImageName = @"yanshuxin" ;
theImageName = [theImageName stringByAppendingPathExtension:@"png" ];
NSLog (@"theImageName = %@" ,theImageName);
//uppercaseString:把所有字母转换为大写
//lowercaseString:把所有字母转换为小写
//capitalizedString:每个单词的首字母转换为大写
NSString *str1 = @"I lovE kongfu" ;
NSLog (@"%@ \n %@ \n %@" ,[str1 uppercaseString],[str1 lowercaseString],[str1 capitalizedString]);
//不可变字符串:单个内容替换
stringByReplacingOccurrencesOfString
NSString *aa = [theImageName stringByReplacingOccurrencesOfString:@"y" withString:@"Y" ];
NSLog (@"%@" ,aa);
//不可变字符串:指定长度内容替换
stringByReplacingCharactersInRange
NSString *aaa = [theImageName stringByReplacingCharactersInRange:NSMakeRange(1 , 3 ) withString:@"ANS" ];
NSLog (@"%@" ,aaa);
//不可变字符串:将原字符串的内容进行分割,分割得到的结果放到一个数组里,作为返回值.
NSString *bb = @"yan,shu,xin" ;
NSArray *ary = [bb componentsSeparatedByString:@"," ];
for (int tmp = 0 ; tmp < 3 ; tmp++) {
NSLog (@"%@" ,ary[tmp]);
}