How to append int value to string?
NSMutableString *selectDay=@"Wed 14 May";
[selectDay stringByAppendingFormat:@"%i", yearNumber];
NSLog(@"%@",selectDay);
I will开发者_如何学运维 tried this one.But it can't append the yearNumber to that String please help me.YearNumber contain 2011.
stringByAppendingFormat:
returns the new string, it does not modify the receiver string. That's why you are getting no change. Try this:
NSMutableString *selectDay=@"Wed 14 May";
NSString *newString = [selectDay stringByAppendingFormat:@"%i", yearNumber];
NSLog(@"%@", newString);
Or this:
NSMutableString *selectDay=@"Wed 14 May";
NSString *newString = [NSString stringWithFormat:@"%@%i", selectDay, yearNumber];
NSLog(@"%@", newString);
EDIT: Actually you don't need mutable string for this. selectDay
should be a normal NSString
.
NSString *selectDay=@"Wed 14 May";
change the below line
[selectDay stringByAppendingFormat:@"%i", yearNumber];
to
selectDay = [NSString stringWithFormat:@"%@%i", selectDay, yearNumber];
definitely it will work ...
You define your variable to be of type NSMutableString *
, but the constant string you're passing is of type NSString *
which is already wrong. There are two solutions: with or without NSMutableString.
NSMutableString *selectDay = [NSMutableString stringWithString:@"Wed 14 May"];
[selectDay appendFormat:@"%i", yearNumber];
NSLog(@"%@", selectDay);
Here the mutable string is generated from the constant string and then it is modified through appending.
NSString *selectDay = @"Wed 14 May";
NSString *newDay = [selectDay stringByAppendingFormat:@"%i", yearNumber];
NSLog(@"%@", newDay);
The point here is that stringByAppendingFormat:
does not modify the original string, it returns a new one. And you simply need to "catch" it in a variable.
Try this:-
NSString *selectDay=@"Wed 14 May";
int yearNumber=2011;
selectDay=[selectDay stringByAppendingFormat:[NSString stringWithFormat:@"%d", yearNumber]];
NSLog(@"%@",selectDay);
NSString *selectDay=@"Wed 14 May";
NSString *appendedString = [NSString stringWithFormat:@"%@ %d",selectDay, yearNumber];
NSLog(@"%@",appendedString);
Try this
You can try:
NSString *selectDay = [NSString stringWithFormat:@"Wed 14 May %d", yearNumber];
精彩评论