<pre name="code" class="objc">#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
构造字符四种方式
NSString 不可变字符串
*/
//1.初始化一个字符串对象
NSString *string1 = @"ibokanwisdom";
// NSString *string2 = [NSString stringWithString:@"ibokanwisdom"];
// NSString *string3 = [[NSString alloc]initWithString:@"ibokanwisdom"];
//使用NSString 的类方法来创建字符串,+(instancerype)stringWithFormat:@"格式化标志符"
NSString *string4 = [NSString stringWithFormat:@"%d,%1.f,hi guys",5,4.5];
//对于用格式化方法来字符串实例来讲,其作用并不仅仅来构造一个字符串对象,它还可以用来做字符串拼接
NSString *string5 = @"Rick";
NSString *string6 = @"666";
NSString *string7 = [NSString stringWithFormat:@"%@%@",string5,string6];
NSLog(@"string=%@",string7);
//从本地文件中读取字符串
NSString *path1=@"/Users/ibokan2/Desktop/OC_04_02/OC_04_02/hello.html";
NSStringEncoding encoding;
NSError *error;
NSString *string8 = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:&error];
NSLog(@"string8=%@",string8);
//从网络读取字符串
NSURL *url=[NSURL URLWithString:@"https://www.alipay.com"];
NSString *string10 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//字符串的操作
NSString *string11 = @"你是逗比么,我都受不了你了";
//字符串属性 长度 length
NSUInteger length = [string11 length];
NSLog(@"length=%lu",length);
//字符串的赋值
NSString *subString1 = [string11 substringToIndex:5];//获取从字符串开头到字符串的 to 的位置的字符串内容,从 0 算起
NSLog(@"%@",subString1);
NSString *subString2 = [string11 substringFromIndex:6];//获取从字符串的 from 位置到结尾的字符串内容,包含 from 位置
NSLog(@"%@",subString2);
NSString *subString3 = [string11 substringWithRange:NSMakeRange(2,2)]; //[2,2]第一个2是从第几个开始,第二个2位输出的长度为几个
NSLog(@"%@",subString3);
//获取字符串中的某个字符
NSString *string12 = @"abcdefg";
char c = [string12 characterAtIndex:0]; //根据字符串中的位置index 来得到一个字符,注意字符串的索引是从 0 开始的
NSLog(@"%c",c);
//字符串比较,如何判断两个字符串是否相等
if (subString2 == subString3) {
//内容和指针都相等,才成立
NSLog(@"subString2 和 subString 相等");
} else {
NSLog(@"subString2 和 subString 不相等");
}
/*如何才能让两个字符串的指针的指针和内容都相等呢?P96
这里需要注意两个概念:浅拷贝和深拷贝
浅拷贝:是指源对象与拷贝对象共用一份实体,仅仅是引用的变量名称不同
深拷贝:是指源对象与拷贝互相独立。其中任何一个对象的改动都不会对另一个对象造成影响
在字符串中,[NSString stringWithString:]方法是用于浅拷贝一个现有字符串产生一个字符串
*/
NSString *subString5 = [NSString stringWithFormat:@"%@",subString2];
NSString *subString4 = [NSString stringWithString:subString2];
if (subString2 == subString4) { //判断内容和指针是否相等
//内容和指针都相等,才成立
NSLog(@"subString2 和 subString4 相等");
} else {
NSLog(@"subString2 和 subString4 相等");
}
//在实际开发过程中,我们通常只需要比较两个字符串的内容是否相等
if ([subString2 isEqualToString:subString3 ]) {
NSLog(@"subString2 和 subString3 内容相等");
} else {
NSLog(@"subString2 和 subString3 内容不想的");
}
//比较两个字符串的大小
NSString *cmpString1 = @"hehehehehehe";
NSString *cmpString2 = @"hahahhahahaha";
NSComparisonResult result = [cmpString1 compare:cmpString2];
switch (result) {
case NSOrderedAscending:
{
NSLog(@"cmpString1 < cmpString2,升序");
break;
}
case NSOrderedSame:
{
NSLog(@"cmpString1 = cmpString2,相等");
break;
}
case NSOrderedDescending:
{
NSLog(@"cmpString1 > cmpString2,降序");
break;
}
default:
break;
}
//根据字符串拿到对应的range
//NSString *string11 = @“你是逗比么,我都受不了你了”;
NSRange range = [string11 rangeOfString:@"逗比"];
NSLog(@"range = %@",[NSValue valueWithRange:range]);
//只输出第一个遇到该字符串的位置 及 该字符串的长度
NSRange rang1 = [string11 rangeOfString:@"你"];
NSLog(@"range1 = %@",[NSValue valueWithRange:rang1]);
//判断字符串是否有xx前缀 或 xx后缀
NSString *string13 = @"http://www.baidu.com";
if ([string13 hasPrefix:@"http://"]) { //hasPrefix判断是否有前缀
NSLog(@"含有此前缀");
} else {
NSLog(@"含有此后缀");
}
//判断是否为 jpg 或 png 后缀
NSString *string14 =@"sdas.png";
if ([string14 hasPrefix:@".jpg"]||[string14 hasSuffix:@"png"]) {
//hasSuffix 判断是否有后缀
NSLog(@"含有此后缀");
} else {
NSLog(@"不含有此后缀");
}
//字符串 拼接
NSString *string15 = @"www.baidu.com";
NSString *string16 = [@"http://" stringByAppendingString:string15];//直接把两个字符串连接起来,调用者前,传进来的字符串在后
NSLog(@"string16 = %@",string16);
//http://img0.bdstatic.com/img/image/shouye/hbmnp003.jpg
NSString *string17 = @"http://img0.bdstatic.com/img/image/shouye";
NSString *string18 = @"hbmnp003.jpg";
// NSString *string19 = [string17 stringByAppendingFormat:@"/%@",string18];//连接两个字符串,并且第二个字符串是按一定格式传进来的
// NSLog(@"string19=%@",string19);
NSString *string20 = [string17 stringByAppendingPathComponent:string18]; //字符串通过添加路径组件
NSLog(@"string20=%@",string20);
NSString *string21 = @"http://img0.bdstatic.com/img/image/shouye/hbmnp003.jpg";
NSString *string22 = [string21 lastPathComponent]; //只取最后一个 / 的字符串部分,最后的路径组件
NSLog(@"string22=%@",string22);
NSString *theImageName = @"Rick";
NSString *theImageName1 =@"png";
NSString *theImagename2 = [theImageName stringByAppendingFormat:@".%@",theImageName1];
NSLog(@"%@",theImagename2); //可以自定义添加符号
NSString *theImagename3 = [theImageName stringByAppendingPathExtension:theImageName1]; //通过扩展名附加路径字符串
NSLog(@"string24=%@",theImagename3);
//字符串的其他采用方法
//1.字符串的转换函数
NSString *str1 = @"111add333";
int a1 = [str1 intValue];
float b1 = [str1 floatValue];
double c1 = [str1 doubleValue];
NSLog(@"a1 = %d",a1);
NSLog(@"b1 =%f",b1);
NSLog(@"c1 =%f",c1);
//2.字符串大小写转换函数
NSString *str2 = @"aaabb";
NSString *str3 = [str2 uppercaseString];//uppercaseString大写字母字符串
NSString *str4 = [str2 lowercaseString];//lowercaseString小写字母字符串
NSLog(@"str3=%@",str3);
NSLog(@"str4=%@",str4);
NSString *str5 = [str2 capitalizedString];//首字母大写
NSLog(@"str=%@",str5);
//字符串的替换函数
NSString *str6 = [str2 stringByReplacingOccurrencesOfString:@"bb" withString:@"dd"];
NSLog(@"str6=%@",str6);
NSString *str7 = [str6 stringByReplacingCharactersInRange:NSMakeRange(1, 2) withString:@"cc"];//将源字符串中range范围内替换成replacement的内容
NSLog(@"str7=%@",str7);
NSString *str8 =@"aa-bb-cc-dd-ee";
NSArray *array = [str8 componentsSeparatedByString:@"-"];//separator 为分隔符,将源字符串的内容进行分隔,分隔得到结果的放到一个数组里,作为返回值
NSLog(@"array=%@",array);
}
return 0;
}
运行结果
2015-12-13 13:59:51.831 OC_LX_02[16147:1043167] string=Rick666
2015-12-13 13:59:51.833 OC_LX_02[16147:1043167] string8=(null)
2015-12-13 13:59:52.315 OC_LX_02[16147:1043167] length=13
2015-12-13 13:59:52.316 OC_LX_02[16147:1043167] 你是逗比么
2015-12-13 13:59:52.316 OC_LX_02[16147:1043167] 我都受不了你了
2015-12-13 13:59:52.316 OC_LX_02[16147:1043167] 逗比
2015-12-13 13:59:52.317 OC_LX_02[16147:1043167] a
2015-12-13 13:59:52.317 OC_LX_02[16147:1043167] subString2 和 subString 不相等
2015-12-13 13:59:52.317 OC_LX_02[16147:1043167] subString2 和 subString4 相等
2015-12-13 13:59:52.317 OC_LX_02[16147:1043167] subString2 和 subString3 内容不想的
2015-12-13 13:59:52.317 OC_LX_02[16147:1043167] cmpString1 > cmpString2,降序
2015-12-13 13:59:52.318 OC_LX_02[16147:1043167] range = NSRange: {2, 2}
2015-12-13 13:59:52.318 OC_LX_02[16147:1043167] range1 = NSRange: {0, 1}
2015-12-13 13:59:52.318 OC_LX_02[16147:1043167] 含有此前缀
2015-12-13 13:59:52.318 OC_LX_02[16147:1043167] 含有此后缀
2015-12-13 13:59:52.319 OC_LX_02[16147:1043167] string16 = http://www.baidu.com
2015-12-13 13:59:52.320 OC_LX_02[16147:1043167] string20=http:/img0.bdstatic.com/img/image/shouye/hbmnp003.jpg
2015-12-13 13:59:52.320 OC_LX_02[16147:1043167] string22=hbmnp003.jpg
2015-12-13 13:59:52.320 OC_LX_02[16147:1043167] Rick.png
2015-12-13 13:59:52.320 OC_LX_02[16147:1043167] string24=Rick.png
2015-12-13 13:59:52.320 OC_LX_02[16147:1043167] a1 = 111
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] b1 =111.000000
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] c1 =111.000000
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] str3=AAABB
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] str4=aaabb
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] str=Aaabb
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] str6=aaadd
2015-12-13 13:59:52.321 OC_LX_02[16147:1043167] str7=accdd
2015-12-13 13:59:52.322 OC_LX_02[16147:1043167] array=(
aa,
bb,
cc,
dd,
ee
)
Program ended with exit code: 0