{
// 字符串
// NSString 不可变字符串
// 1.初始化方法
//初始化一个空的字符串
NSString *string = [[NSString alloc] init];
// NSLog(@"%@", string);
NSString *string1 = @"asd";//字面量
NSLog(@"%@", string1);
//根据字符串初始化
NSString *string2 = [[NSString alloc] initWithString:string];
NSLog(@"yu%@", string2);
//根据格式串初始化
NSString *string3 = [[NSString alloc] initWithFormat:@"男哥%d %f", 10, 11.3];
// 2.便利构造器
// 创建一个空的字符串
NSString *string4 = [NSString string];
// 通过一个字符串创建字符串
NSString *string5 = [NSString stringWithString:string3];
// 创建一个格式串初始化
NSString *string6 = [NSString stringWithFormat:@"%@--%d", string5, 10];
NSLog(@"%@", string6);
// 3.字符串长度
NSLog(@"%lu", string6.length);
// 4.获取字符串中的字符
NSLog(@"%c", [string1 characterAtIndex:1]);
// 5.判断字符串是否相等
if ([string1 isEqualToString:string1]) {
NSLog(@"相等");
} else {
NSLog(@"不相等");
}
// 6.拼接字符串
string1 = [string1 stringByAppendingString:string6];
NSLog(@"%@", string1);
// 7.替换字符串
//1.直接替换字符串
[string3 stringByReplacingOccurrencesOfString:@"男" withString:@"ff"];
NSLog(@"%@", string1);
//2.给定范围替换
NSRange range = {1, 2};
[string6 stringByReplacingCharactersInRange:range withString:@"那可是电脑"];
NSLog(@"%@", string6);
// 8.将字符串转换成基本数据类型
// 这个字符串只能包含基本数据类型的数据
NSString *str = @"123";
// intValue floatValue integerValue
NSLog(@"%d", str.intValue);
// 9.大小写转换
NSLog(@"%@", [string1 uppercaseString]);//大写
NSLog(@"%@", [string1 lowercaseString]);//小写
// 首字母大写
NSString *string7 = @"i love you";
NSLog(@"%@", [string7 capitalizedString]);
// 10.判断前缀后缀
NSString *url = [NSString stringWithFormat:@"www.sdf.com"];
// [string1 hasPrefix:@"NSString"] == 1 ? NSLog(@"YES") : NSLog(@"NO");
// [string1 hasSuffix:@".txt"] == 1 ? NSLog(@"YES") : NSLog(@"NO");
if ([url hasPrefix:@"www"]) {
NSLog(@"前缀");
} if ([url hasSuffix:@"com"]) {
NSLog(@"后缀");
}
// 12.比较字符串
NSString *string01 = @"qwer";
NSString *string02 = @"qwer";
BOOL result = [string01 isEqualToString:string02];
NSLog(@"result:%d", result);
// [string01 compare:string02];
// NSString *String9 = @"NSStringInformation.txt";
// [String9 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
// [String9 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
//1.可变字符串创建
NSMutableString *mutableString = [NSMutableString stringWithFormat:@"asdasd"];
NSMutableString *mutableString1 = [NSMutableString stringWithFormat:@"asdasdas"];
[mutableString setString:mutableString1];
NSLog(@"%@", mutableString);
// 2.拼接
[mutableString appendString:@"qwer"];
NSLog(@"%@", mutableString);
// 3.插入
[mutableString insertString:@"tt" atIndex:0];
NSLog(@"%@", mutableString);
// 4.删除
[mutableString deleteCharactersInRange:NSMakeRange(3, 5)];
NSLog(@"%@", mutableString);
// 5.替换 (将一个范围的字符串替换成另一个字符串)
[mutableString replaceCharactersInRange:NSMakeRange(0, 2) withString:@"HH"];
NSLog(@"%@", mutableString);
return 0;
}