标签:
// 1.获取字符串长度 NSString *string1 = [NSString stringWithFormat:@"jkaruu"]; // NSLog(@"stringLength = %ld", string1.length); // 2.获取指定位置字符 unichar c = [string1 characterAtIndex:0]; NSLog(@"c = %c", c); // 3.判断字符串的前缀 后缀 BOOL isHasSuffix = [string1 hasSuffix:@"www."]; NSLog(@"isHasSuffix = %d", isHasSuffix); BOOL isHasPerfix = [string1 hasPrefix:@"jk"]; NSLog(@"isHasPerfix = %d", isHasPerfix); // 4.查找某个字符串在另一个字符串的范围 NSString *string2 = @"蓝鸥科技有限公司"; NSString *string3 = @"蓝鸥科技"; // NSRange 结构体,表示范围 // location:起始位置 // length:长度 NSRange range = [string2 rangeOfString:string3]; NSLog(@"location = %ld, length = %ld", range.location, range.length); // 5.字符串截取 NSString *string4 = @"hello world kitty"; NSRange range1 = NSMakeRange(0, 5); NSString *string5 = [string4 substringWithRange:range1]; NSLog(@"string5 = %@", string5); // 从指定位置向前截取,不包含指定位置的下标 NSString *string6 = [string4 substringToIndex:7]; NSLog(@"string6 = %@", string6); // 从指定位置向后截取,包含指定位置的下标 NSString *string7 = [string4 substringFromIndex:5]; NSLog(@"string7 = %@", string7); // 6.字符串拼接 NSString *string8 = @"zhangsan"; NSString *string9 = @"lisi"; NSString *stringL = [string8 stringByAppendingString:string9]; NSLog(@"stringl = %@", stringL); // 7.字符串替换 NSString *string10 = @"wangwu"; NSString *string11 = [string10 stringByReplacingOccurrencesOfString:string10 withString:@"liuaoran"]; NSLog(@"string11 = %@", string11); // 8.字符串比较 NSInteger result = [string10 compare:string11]; NSLog(@"result = %ld", result); // 判断两个字符串是否相等 BOOL isEqual = [string10 isEqualToString:string11]; NSLog(@"isEqual = %d", isEqual); // 9.字符串和数值的类型转换 NSString *s1 = @"123"; NSInteger strValue = [s1 integerValue]; NSLog(@"strValue = %ld", strValue); // 10.字符串大小写之间的转换 // 首字母大写 string10 = [string10 capitalizedString]; NSLog(@"string10 = %@", string10); // 全部字母大写 string10 = [string10 uppercaseString]; NSLog(@"string10 = %@", string10); // 全部字母小写 string10 = [string10 lowercaseString]; NSLog(@"string10 = %@", string10);
// 1.拼接字符串 NSMutableString *mString1 = [NSMutableString stringWithFormat:@"zhangsan"]; [mString1 appendString:@"666"]; NSLog(@"mString1 = %@", mString1); // 2.插入字符串 [mString1 insertString:@"," atIndex:8]; NSLog(@"mString1 = %@", mString1); // 3.删除字符串 [mString1 deleteCharactersInRange:NSMakeRange(3, 2)]; NSLog(@"mString1 = %@", mString1); // 4.字符串替换 [mString1 replaceCharactersInRange:NSMakeRange(3, 3) withString:@"ng"]; NSLog(@"mString1 = %@", mString1); // 5.重置字符串 [mString1 setString:@"zhangsan"]; NSLog(@"mString1 = %@", mString1);
// 把数值对象转为基本数据类型 NSNumber *number1 = [[NSNumber alloc] initWithInt:11]; double a = [number1 doubleValue]; NSLog(@"%f", a); NSLog(@"%@", number1);
3、NSNumber字面量
// 创建对象 NSRange range0 = NSMakeRange(0, 3); NSValue *value1 = [NSValue valueWithRange:range0]; NSLog(@"value1 = %@", value1); // 把指针转为数值对象 int b = 10; int *p = &b; NSValue *value2 = [NSValue valueWithPointer:p]; NSLog(@"value2 = %@", value2); // 把数值对象转为结构体 NSRange range3 = [value1 rangeValue]; NSLog(@"location = %ld, length = %ld", range3.location, range3.length); // 把数值对象转为指针 int *q = [value2 pointerValue]; NSLog(@"q = %d", *q);
标签:
原文地址:http://www.cnblogs.com/soley/p/5311493.html