如何将int值附加到字符串?

时间:2021-09-15 19:52:36
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.

我要试试这个。但是它不能把年号附加到那个字符串上请帮帮我。YearNumber包含2011。

6 个解决方案

#1


6  

stringByAppendingFormat: returns the new string, it does not modify the receiver string. That's why you are getting no change. Try this:

stringByAppendingFormat:返回新的字符串,它不修改接收字符串。这就是为什么你没有得到改变。试试这个:

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.

编辑:实际上不需要可变字符串。selectDay应该是一个普通的NSString。

NSString  *selectDay=@"Wed 14 May";

#2


2  

change the below line

改变以下线

[selectDay stringByAppendingFormat:@"%i", yearNumber];

to

selectDay = [NSString stringWithFormat:@"%@%i", selectDay, yearNumber];

definitely it will work ...

肯定会成功的……

#3


1  

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 *,但是您传递的常量字符串是NSString *,这是错误的。有两种解决方案:带或不带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.

这里的要点是stringByAppendingFormat:不修改原始字符串,它返回一个新的字符串。你只需要在变量中“捕获”它。

#4


1  

Try this:-

试试这个:

NSString   *selectDay=@"Wed 14 May";
    int yearNumber=2011;
    selectDay=[selectDay stringByAppendingFormat:[NSString stringWithFormat:@"%d", yearNumber]];
    NSLog(@"%@",selectDay);

#5


1  

   NSString *selectDay=@"Wed 14 May";
    NSString *appendedString = [NSString stringWithFormat:@"%@ %d",selectDay, yearNumber];

    NSLog(@"%@",appendedString);

Try this

试试这个

#6


0  

You can try:

你可以尝试:

NSString *selectDay = [NSString stringWithFormat:@"Wed 14 May %d", yearNumber];

#1


6  

stringByAppendingFormat: returns the new string, it does not modify the receiver string. That's why you are getting no change. Try this:

stringByAppendingFormat:返回新的字符串,它不修改接收字符串。这就是为什么你没有得到改变。试试这个:

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.

编辑:实际上不需要可变字符串。selectDay应该是一个普通的NSString。

NSString  *selectDay=@"Wed 14 May";

#2


2  

change the below line

改变以下线

[selectDay stringByAppendingFormat:@"%i", yearNumber];

to

selectDay = [NSString stringWithFormat:@"%@%i", selectDay, yearNumber];

definitely it will work ...

肯定会成功的……

#3


1  

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 *,但是您传递的常量字符串是NSString *,这是错误的。有两种解决方案:带或不带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.

这里的要点是stringByAppendingFormat:不修改原始字符串,它返回一个新的字符串。你只需要在变量中“捕获”它。

#4


1  

Try this:-

试试这个:

NSString   *selectDay=@"Wed 14 May";
    int yearNumber=2011;
    selectDay=[selectDay stringByAppendingFormat:[NSString stringWithFormat:@"%d", yearNumber]];
    NSLog(@"%@",selectDay);

#5


1  

   NSString *selectDay=@"Wed 14 May";
    NSString *appendedString = [NSString stringWithFormat:@"%@ %d",selectDay, yearNumber];

    NSLog(@"%@",appendedString);

Try this

试试这个

#6


0  

You can try:

你可以尝试:

NSString *selectDay = [NSString stringWithFormat:@"Wed 14 May %d", yearNumber];