1.创建字符串对象
//(1)使用初始化方法
NSInteger number = 10;
NSString *str = @"Frank";
NSString *str1 = [[NSString alloc] initWithFormat:@"lanou%ld%@", number, str];
NSLog(@"str1 = %@", str1);
2.能够将C语言的字符串转化为OC的字符串对象.
NSString *str1 = [[NSString alloc] initWithUTF8String:"6plus"];
NSString *str2 = [[NSString alloc] initWithFormat:@"iPhone%@", str1];
NSLog(@"str2 = %@", str2);
(2)便利构造器
NSString *str4 = [NSString stringWithUTF8String:"6plus"];
NSString *str3 = [NSString stringWithFormat:@"iPhone%@", str4];
NSLog(@"str3 = %@", str3);
3.求字符串长度
NSString *str1 = [NSString stringWithFormat:@"lanouhenan"];
NSUInteger length = [str1 length];
NSLog(@"%lu", length);
4..判断一个字符串是否以http开头,以png结尾.如果是,则输出是一个有效连接,否则输出链接错误.
NSString *str2 = [NSString stringWithFormat:@"http://www.lanou3g.com/picture/duck.png"];
//(1)是否以http开头
BOOL isPrefix = [str2 hasPrefix:@"http"];
//(2)是否以png结尾
BOOL isSuffix = [str2 hasSuffix:@"png"];
if (isPrefix && isSuffix) {
NSLog(@"是一个有效的链接");
} else {
NSLog(@"无效链接");
}
4.判断两个字符串是否相等
NSString *str1 = @"lanou";
NSString *str2 = [NSString stringWithFormat:@"lanou"];
//(1)判断地址是否相同
if (str1 == str2) {
NSLog(@"两个对象指向同一个空间");
} else {
NSLog(@"两个对象指向不同的空间");
}
//(2)判断内容是否相同
if ([str1 isEqualToString:str2]) {
NSLog(@"两个字符串内容相同");
} else {
NSLog(@"两个字符串内容不同");
}
5.获取子串
NSString *str1 = [[NSString alloc] initWithFormat:@"lanouhenan"];
//获取lanou
NSString *subStr1 = [str1 substringToIndex:5];
NSLog(@"subStr1 = %@", subStr1);
//获取henan
NSString *subStr2 = [str1 substringFromIndex:5];
NSLog(@"subStr2 = %@", subStr2);
//获取ouhe
//快速创建NSRange结构体变量
//NSRange location 开始位置 length 长度
NSString *subStr3 = [str1 substringWithRange:NSMakeRange(3, 4)];
NSLog(@"subStr3 = %@", subStr3);
6.拼接字符
NSString *str = [NSString stringWithFormat:@"蓝欧"];
(1)拼接上henan
NSString *str1 = [str stringByAppendingString:@"河南"];
NSLog(@"str1 = %@", str1);
(2)输入班级号拼接上,比如,输入7,输出lanouhenan7班
int number = 0;
printf("请输入班级号:\n");
scanf("%d", &number);
NSString *str2 = [str stringByAppendingFormat:@"河南%d班", number];
NSLog(@"str2 = %@", str2);*/
7.替换字符串
NSString *str1 = [[NSString alloc] initWithFormat:@"lanouhenan"];
//(1)将a 替换成 Frank
NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"a" withString:@"Frank"];
NSLog(@"str2 = %@", str2);
//(2)将 henan 替换成 beijing
//first method
NSString *str3 = [str1 stringByReplacingOccurrencesOfString:@"henan" withString:@"beijing"];
NSLog(@"str3 = %@", str3);
//second method
NSString *str4 = [str1 stringByReplacingCharactersInRange:NSMakeRange(5, 5) withString:@"beijing"];
NSLog(@"str4 = %@",str4);
8.字符串比较
NSString *str1 = [[NSString alloc] initWithFormat:@"Frank"];
NSString *str2 = [[NSString alloc] initWithFormat:@"Duck"];
NSComparisonResult result = [str1 compare:str2];
NSLog(@"result = %ld", result);
9.大小写转换操作
NSString *str1 = [NSString stringWithFormat:@"Lanou is big company"];
//(1)全部大写
NSString *upperStr = [str1 uppercaseString];
NSLog(@"upperStr = %@", upperStr);
//(2)全部小写
NSString *lowerStr = [str1 lowercaseString];
NSLog(@"lowerStr = %@", lowerStr);
//(3)单词首字母大写
NSString *capitalStr = [str1 capitalizedString];
NSLog(@"capital = %@", capitalStr);
NSString(不可变字符串) 与 NSMutableString(可变字符串)的区别
可变字符串是在不可变字符串的基础上增加了对原有对象增删改的操作, 而不可变字符操作的是副本.
1.创建可变字符串对象
Capacity 大小
NSMutableString *str1 = [[NSMutableString alloc] initWithCapacity:1];
NSMutableString *mStr2 = [NSMutableString stringWithCapacity:1];
/2.修改字符串(对原有的字符串对象做修改).
NSMutableString *mStr3 = [NSMutableString stringWithFormat:@"lanouhenan"];
//(1)拼接字符串
[mStr3 appendFormat:@"Frank%d",1]; //可以拼接字符和格式
NSLog(@"mStr3 = %@", mStr3);
[mStr3 appendString:@"Duck"]; //只可以拼接字符串
NSLog(@"mStr3 = %@", mStr3);
//当子类使用从父类继承过来的修改方法时,内部实现依然是操作副本,不会对原有对象做修改.
NSString *Str1 = [mStr3 stringByAppendingString:@"beijing"];
NSLog(@"mStr3 = %@", Str1);
(2)插入字符串
[mStr3 insertString:@"38" atIndex:[mStr3 length]];
NSLog(@"mStr3 = %@", mStr3);
(3)删除字符串
[mStr3 deleteCharactersInRange:NSMakeRange(5, 5)];
NSLog(@"mStr3 = %@", mStr3);
(4)替换字符串 Duck 换成 Kris
[mStr3 replaceCharactersInRange:NSMakeRange(11, 4) withString:@"Kris"];
NSLog(@"mStr3 = %@", mStr3);
(5)重置字符串
[mStr3 setString:@"beijinglanou"];
NSLog(@"mStr3 = %@", mStr3);