簡體   English   中英

如何在Objective-C中連接兩個數字

[英]How to concatenate two numbers in Objective-C

如何連接兩個數字,如7和6,得到數字76,或3和3,所以結果是33,在objective-c?

沒有內置符號來連接數字。 但是,您可以通過以下方式完成此操作:

int first; /* Assuming this is initialized to the first number */
int second; /* Assuming this is initalized to the second number */
int myVal = [[NSString stringWithFormat:@"%d%d",first, second] intValue];

FirstNum * 10 + secondNum :-)

這不是數字操作,而是字符串連接。

Objective-C中的快捷方式用於連接NSStrings

如果你想要兩個數字x和y添加到xy,你可以這樣做

10 * x + y。

7和6

7 * 10 + 6 = 76

我對Objective-c了解不多,但我會說:

  • 如果從數組中獲取數字,例如nums = array(7,6),則初始化result = 0然后對它們進行foreach。 對於您找到的每個值,請執行:res = res * 10 + value。 最后,即使你有7個數字連接,你也會得到正確的結果。 即:

    數組nums =數組(7,6,8,9); int res = 0; int值; foreach(nums中的值)res = res * 10 + value;

  • 如果你可以使用字符串,只需像上面建議的那樣連接它們。 可能有一個函數來連接數組中的所有值以使其靈活。

希望能幫助到你

C 1-6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM