不变字符串NSSting
//使用实例方法创建NSSting的对象
NSString *string1 = [[NSString alloc] initWithFormat:@"姓名:%@", @"hfjs"];
NSString *string2 = [[NSString alloc] initWithFormat:@"姓名"];
NSLog(@"%@ %@", string1, string2);
int a = 10;
NSString *string3 = [[NSString alloc] initWithFormat:@"%d", a];
NSLog(@"%@", string3);
//使用类方法创建NSString对象
NSString *string4 = [NSString stringWithFormat:@"蓝鸥"];
NSLog(@"%@", string4);
//直接赋值
NSString *string5 = @"safsdafsafas";
NSLog(@"%@", string5);
//获取字符串长度
NSUInteger string2Length = [string2 length];
NSLog(@"%ld", string2Length);
//判断字符串前缀
BOOL result1 = [string1 hasPrefix:@"姓"];
NSLog(@"%d", result1);
//判断字符串后缀
BOOL result2 = [string5 hasSuffix:@"s"];
NSLog(@"%d", result2);
//查找字符串所在位置
NSRange range1 = [string5 rangeOfString:@"dafs"];
NSLog(@"location:%ld length:%ld”, range1.location, range1.length);
//要查找的字符串的首字符位置,和查找的长度
//字符串截取
NSString *string6 = [string5 substringFromIndex:2];
NSLog(@"%@", string6);
//从指定第几个字符开始截取,到最后结束
NSString *string7 = [string5 substringToIndex:6];
//从第一位字符开始,截取到指定位置结束
NSLog(@"%@", string7);
// NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>) 传位置 和 长度
NSString *string8 = [string5 substringWithRange:NSMakeRange(2, 5)];
//区间查找
NSLog(@"%@", string8);
//拼接字符串:append
NSString *string9 = [string1stringByAppendingString:@".sdf"];
NSLog(@"%@", string9);
//替换字符串:replacing
NSString *tihuan = [string1stringByReplacingOccurrencesOfString:@“姓名” withString:@"蓝鸥3g"];
NSLog(@"%@", tihuan);
//字符串比较:compare
NSString *b = @"abc";
NSString *c = @"Abc";
NSComparisonResult result = [b compare:c];
switch (result) {
case NSOrderedAscending:
NSLog(@"b < c");
break;
case NSOrderedDescending:
NSLog(@"b > c");
break;
case NSOrderedSame:
NSLog(@"b = c");
break;
default:
break;
}
//字符串和数值类型转换:int/double/float
NSString *d = @"6431";
NSInteger intStr = [d integerValue];
NSLog(@"%ld", intStr);
double douleStr = [@"524.5265" doubleValue];
NSLog(@"%lf", douleStr);
//大小写转换:low/up
NSString *a1 = @"mY NamE iS ZhanG QianG";
NSString *uppercaseStr = [a1 uppercaseString];
NSString *lowercaseStr = [a1 lowercaseString];
NSString *capitalizedStr = [a1 capitalizedString];
NSLog(@"%@", uppercaseStr);
NSLog(@"%@", lowercaseStr);
NSLog(@"%@", capitalizedStr);