Objective_C_字符串的使用

创建字符串

//通过便利构造器创建初始化
NSString *person = [NSString stringWithFormat:@"我今年%d岁了",16];

//%lu无符号NSUInterager

unichar c = [@"greger" characterAtIndex:0];
    NSLog(@"----%c",c);

两个字符串比较内容

NSString *name1 = @"香房";
NSString *name2 = @"香房";
if(name1 isEqualString name2){
NSLog(字符串相同);
}

调用两个方法来比较字符串,-1前面的小于后面的

NScomparisonresult compareResult = [@"abc" compareResult @"cbf"];
NSLog(@"%ld",compareResult);

字符串的截取有三种方法:1⃣️

NSString *numberString = [NSString substringFromIndex:1] ;
NSLog(@"%@",numberString);
2⃣️
NSString *numberString2 = [NSString substringToIndex:1];
NSLog(@"%@",numberString2);
3⃣️
NSString *numberString3 = [@"sdfffggggggg" substringRang:NSMakeRang(2,4)];
NSLog(@"%@",numberString3);

字符串的拼接

NSString *appendstring = [@"jhdijw" stringByAppendingString @"few"];
 NSLog(@"输出appendeing:%@",appendString);

字符串的后面追加一个格式化字符串

NSString *appendFormat = [@"dsewe" stringByAppendFormat :@"%ld",123];
NSLog(%@,appendFormat);

截取一个字符串在一段的字符串的位置和长度

NSRang rang = [contentString rangeOfString:@"ewfree"];
NSLog(@"rang:loc = %lu,len = %lu",rang.location,rang.length);

字符串的替换

NSString *replace = [@"derwer4tr" stringByReplacingCharacterinRange:NSMakeRaanng(3,3) withString:@"jief"];
 NSLog(@"字符串替换%@",replace);

字符串转数字(NSnumber是数字类)

NSInterge number = [@"1243" inValue];
NSLog(@"%ld",number+1);

其他数据类型转化为字符串

NSInteger number = 123;
NSLog(@"其他数据类型转化为字符串",[NSString stringWithFormat:@"%ld",number);

字符串转成大写.小写,首字母大写;

NSString *UppercaseString = [@"asd" UppercaseString];
    NSLog(@"转成大写:%@",uppercaseString);
NSString *lower= [@“ASD” lowercaseString];
NSLog(@"转成小写:%@",lowercaseString);
NSString *capitalizedString = [NSString capitalizedString];
 NSLog(@"字符串首字母大写%@",capitalizedString);

字符串的前缀-----后缀(hasSuffix)

if([@"dsf" hasPerfix:@"d"]){
NSLog(@"是前缀");
}
else{
NSLog(@"不是前缀");
}

可变字符串 遍历字符串

NSMutableString *string = [NSMutableString stringWithString:@"jmioo"];

插入字符串到索引位置---------删除

NSString *insert = @"dswedwq";
NSString *insert1=[insert insertString:@"ksow" atIndex:2];
  NSLog(@"插入字符串%@",insert1);
NSString *delectCharacters = [insert deleteCharactersInRange(2,1)];

(NsNumber是数字类)

NSNumber *boolNumber = [NSNumber numberWithBool:YES];
NSNumber *intNumber = [NSNumber numberWithInt:23];
NSInteger integer = intNumber.integerValue;

number的字面量写法-------//值对象

NSNumber *numberObject = @10;
int x=1;
NSNumber *numberObject1 = @(x);
//值对象

NSValue *rangeValue = [NSValue valueWithRang:NSMakeRange(10,10)];
 NSLog(@"%@",rangValue);
NSRang rang1 = rang value.rangValue;

你可能感兴趣的:(Objective_C_字符串的使用)