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.
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";
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);
stringByAppendingFormat: does not modify selectDay, it returns a new string (which is simply lost as it's not assigned to a variable). NSString *selectDay=@"Wed 14 May";
NSString *appendedString = [NSString stringWithFormat:@"%@ %d",selectDay, yearNumber];
NSLog(@"%@",appendedString);
Try this