xcode 在 Objective C 中连接整数和字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14314231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate integers and strings in Objective C
提问by Zolt
Please forgive the simplicity of the question. I'm completely new to Objective C.
请原谅问题的简单性。我对目标 C 完全陌生。
I'd like to know how to concatenate integer and string values and print them to the console.
我想知道如何连接整数和字符串值并将它们打印到控制台。
This is what I'd like for my output:
这是我想要的输出:
10 + 20 = 30
In Java I'd write this code to produce the needed results:
在 Java 中,我会编写此代码以生成所需的结果:
System.Out.Println(intVarWith10 + " + " + intVarWith20 + " = " + result);
Objective-C is quite different. How can we concatenate the 3 integers along with the strings in between?
Objective-C 完全不同。我们如何将 3 个整数与中间的字符串连接起来?
回答by svrushal
You can use following code
您可以使用以下代码
int iFirst,iSecond;
iFirst=10;
iSecond=20;
NSLog(@"%@",[NSString stringWithFormat:@"%d + %d =%d",iFirst,iSecond,(iFirst+iSecond)]);
回答by ColinE
Take a look at NSString
- it has a method stringWithFormatthat does what you require. For example:
看看NSString
- 它有一个方法stringWithFormat 可以满足您的要求。例如:
NSString* yString = [NSString stringWithFormat:@"%d + %d = %d",
intVarWith10, intVarWith20 , result];
回答by Krishnabhadra
You can use C style syntax, with NSLog (If you just need to print)
您可以使用 C 风格的语法,使用 NSLog(如果您只需要打印)
NSLog(@"%d+%d=%d",intvarWith10,intvarWith20,result);
If you want a string variable holding the value
如果你想要一个保存值的字符串变量
NSString *str = [NSString stringWithFormat:@"%d+%d=%d",intvarWith10,intvarWith20,result];
回答by Daniel García
You have to create an NSString with format and specify the data type.
您必须创建一个带有格式的 NSString 并指定数据类型。
Something like this :
像这样的事情:
NSInteger firstOperand=10;
NSInteger secondOperand=20;
NSInteger result=firstOperand+secondOperand;
NSString *operationString=[NSString stringWithFormat:@"%d + %d = %d",firstOperand,secondOperand,result];
NSLog(@"%@",operationString);
NSString with format follows the C printf syntax
带有格式的 NSString 遵循 C printf 语法
回答by NSCry
Check below code :
检查以下代码:
int i = 8;
NSString * tempStr = [NSString stringWithFormat@"Hello %d",i];
NSLog(@"%@",tempStr);
回答by Nirav
I strongly recommend you this linkObjective-C Reference.
我强烈推荐你这个链接Objective-C Reference。
The Objective-C int data type can store a positive or negative whole number. The actual size or range of integer that can be handled by the int data type is machine and compiler implementation dependent.
Objective-C int 数据类型可以存储正整数或负整数。int 数据类型可以处理的整数的实际大小或范围取决于机器和编译器实现。
So you can store like this.
所以你可以这样存储。
int a,b;
国际a,b;
a= 10; b= 10;
一 = 10; b=10;
then performing operation you need to first understand NSString
.
然后执行操作你需要先了解NSString
。
C style character strings are composed of single byte characters and therefore limited in the range of characters that can be stored.
C 风格的字符串由单字节字符组成,因此可以存储的字符范围受到限制。
int C = a + b; NSString *strAnswer = [NSString stringWithFormat:@"Answer %d + %d = %d", a , b, c];
int C = a + b; NSString *strAnswer = [NSString stringWithFormat:@"Answer %d + %d = %d", a , b, c];
NSLog(@"%@",strAnswer)
NSLog(@"%@",strAnswer)
Hope this will help you.
希望这会帮助你。