// NSSting
//initWithFormat: 方法 通过给定格式化字符串 复制 给字符串对象赋值
NSString *str1 = [[NSString alloc] initWithFormat:@"just so so,%@%d",@"iPhone",6];
NSLog(@"%@",str1);
//stringWithFormat : 方法 其实内部就是 alloc + initWithFormat;
NSString *str2 = [NSString stringWithFormat:@"使用便利构造器创建字符串"];
NSLog(@"%@",str2);
//使用 initWithUTF8String: 将C语言的字符串转化为OC字符串对象
NSString *str3 = [[NSString alloc] initWithUTF8String:"我是C语言字符串,所以前面不加@"];
NSLog(@"%@",str3);
//
NSString *str4 = [NSString stringWithUTF8String:"使用便利构造器,将C语言字符串,转化为OC字符串对象"];
NSLog(@"%@",str4);
NSString *str6 = @"难";
NSString *str5 = @"难";
//1.创建一个字符串对象 http://www.lanou3g.com
NSString *str = [[NSString alloc] initWithFormat:@"http://www.lanou3g.com"];
//2.获取长度
NSInteger length = [str length];
NSLog(@"length = %ld",length);
//3.大写
NSString *upperStr = [str uppercaseString];
NSLog(@"%@",upperStr);
//4.小写
NSString *lowerStr = [upperStr lowercaseString];
NSLog(@"%@",lowerStr);
//5.判定字符串开头
BOOL isBegin = [str hasPrefix:@"http:"];
NSLog(@"%d",isBegin);
//6.结尾
BOOL isEnd = [str hasSuffix:@".com"];
NSLog(@"%d",isEnd);
//7.拼接
NSString *appendStr = [str stringByAppendingFormat:@".cn"];
NSLog(@"%@",appendStr);
//8.替换
//!! 替换的是字符串 不是字符
NSString *replaceStr = [str stringByReplacingOccurrencesOfString:@"w" withString:@"mm"];
NSLog(@"%@",replaceStr);
//9.比较
NSInteger result = [@"aa" compare:@"bb"];
NSLog(@"%ld",result);
//10.判断相等
BOOL isEqual = [@"ab" isEqualToString:@"ab"];
NSLog(@"%d",isEqual);
//11.截取
//substringFromIndex: 从给定下标开始截取到字符串结束(包含当前下标)
NSString *subStr1 = [str substringFromIndex:11];
NSLog(@"%@",subStr1);
//substringToIndex: 从开始截取到给定下标(不包含当前下标)
NSString *subStr2 = [str substringToIndex:11];
NSLog(@"%@",subStr2);
NSRange range1 = {11,7};
//OC中提供了很多快速创建结构体的方法 NSMake + 结构体名字 (NSMakeRange());
NSString *subStr3 = [str substringWithRange:NSMakeRange(11, 7)];
NSLog(@"%@",subStr3);
//14.获取字符串的范围
NSRange range = [str rangeOfString:@"www"];
NSLog(@"%ld,%ld",range.location,range.length);
//将标量 range转化为字符串对象
NSString *rangeStr = NSStringFromRange(range);
NSLog(@"%@",rangeStr);
//15.将基本数据类型 转化为字符串对象
NSString *intStr = [[NSString alloc] initWithFormat:@"%c",‘a‘];
NSLog(@"%@",intStr);
NSLog(@"%@",str);
对于NSString对象来说,是不可变的 不管是拼接 替换 截取还是大小写转换 都是对其副本进行操作,其本身是不变的
NSMutableString
创建可变字符串对象
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"love yangYing"];
NSLog(@"%@",str);
//插入
[str insertString:@"jinPeng " atIndex:0];
NSLog(@"%@",str);
//拼接
[str appendFormat:@" and haiYang"];
NSLog(@"%@",str);
//删除
[str deleteCharactersInRange:NSMakeRange(12, 13)];
NSLog(@"%@",str);
//重置
[str setString:@"金鹏 爱 海洋"];
NSLog(@"%@",str);
给定一个图片文件名,判断字符串中是否以“png”结尾,如果是就替换成“jpg”,如果 不是,就拼接”.jpg”。 123
创建可变字符串
NSMutableString *test = [[NSMutableString alloc] initWithFormat:@"123png"];
//判断是否是png结尾
BOOL isEnd = [test hasSuffix:@"png"];
//判断
if (isEnd) {
//替换
//删除png
[test deleteCharactersInRange:NSMakeRange([test length] - 3, 3)];
//再拼接png
[test appendFormat:@"jpg"];
}else
{
//拼接
[test appendFormat:@".jpg"];
}
NSLog(@"%@",test);
//(2)判断字符串@"aBcD_EfGk"中是否有EfGk,如果有将EfGk转换为WXYZ,并让字符串的大写字母转换成小写,输出修改完成的字符串
//方法1 : 直接替换
//创建字符串对象
NSString *str = @"aBcD_EfGk";
//替换
NSString *replaceStr = [str stringByReplacingOccurrencesOfString:@"EfGk" withString:@"WXYZ"];
//转化为小写
NSString *lowerStr = [replaceStr lowercaseString];
NSLog(@"%@",lowerStr);
//方法2
//判断字符串是否存在
NSRange range = [str rangeOfString:@"EfGk"];
NSLog(@"%ld", range.length);
if (range.length > 0) {
NSLog(@"存在");
}else
{
NSLog(@"不存在");
}