#pragma mark - 初始化构造字符串 NSString *str = [[NSString alloc]initWithString:@"Hell kitty"]; NSString *str2 = [[NSString alloc]initWithFormat:@"zzzz%d", 110]; NSLog(@"%@", str); NSLog(@"%@", str2); #pragma mark - 便利构造器的初始化方法 NSString *str3 = [NSString stringWithString:@"jjjjj"]; NSString *str4 = [NSString stringWithFormat:@"我是%@", @"张杰"]; NSLog(@"%@", str3); NSLog(@"%@", str4); #pragma mark - 常用操作 //获取字符串长度 NSString *str5 = @"zhang jiemiaoshuang"; NSLog(@"%lu", [str5 length]); //判断字符串是否以制定字符串开始或者结束 NSLog(@"%d", [str5 hasPrefix:@"zhang2"]); NSLog(@"%d", [str5 hasSuffix:@"ang"]); //判断某个字符串是否在里面(字符串截取) NSLog(@"%@", [str5 substringFromIndex:5]); NSLog(@"%@", [str5 substringToIndex:5]);// NSLog(@"%@", [str5 substringWithRange:NSMakeRange(2, 5)]);//从哪里开始 截取多少位 //字符串拼接 NSLog(@"%@", [str5 stringByAppendingString:@"哈哈哈"]); NSLog(@"%@", [str5 stringByAppendingFormat:@"%@",@"哈哈哈"]); //字符串比较 NSString *str6_1 = @"zhangjie"; NSString *str6_2 = @"miaoshuang"; NSComparisonResult res = [str6_1 compare:str6_2]; if ( res== NSOrderedAscending) { NSLog(@"后面大"); }else if(res == NSOrderedSame){ NSLog(@"相等"); }else{ NSLog(@"前面大"); } //字符串替换 str5 = [str5 stringByReplacingOccurrencesOfString:@"miaoshuang" withString:@"苗爽"]; NSLog(@"%@", str5); //大小写转换 str5 = [str5 uppercaseString]; NSLog(@"%@", str5); str5 = [str5 lowercaseString]; NSLog(@"%@", str5); str5 = [str5 capitalizedString]; NSLog(@"%@", str5); //字符串和数值类型的装换 //数值类型-->字符串 NSString *str7 = [NSString stringWithFormat:@"%d", 4]; //字符串-->数值 NSInteger i = [str7 integerValue]; NSLog(@"%ld", i); #pragma mark - NSMutableString(可变字符串) NSMutableString *mutableStr = [NSMutableString stringWithString:@"zhangjie"]; //拼接字符串 [mutableStr appendString:@"love miaoshuang"]; NSLog(@"%@", mutableStr); //插入字符串 [mutableStr insertString:@"hahah" atIndex:10]; NSLog(@"%@", mutableStr); //删除字符串 [mutableStr deleteCharactersInRange:NSMakeRange(2, 10)]; NSLog(@"%@", mutableStr);
时间: 2024-11-09 23:09:58