在C#中串连整数 [英] Concatenate integers in C#
问题描述
是否有一个的便宜的方式来串联整数CSHARP
Is there an inexpensive way to concatenate integers in csharp?
?例如:1039&安培; 7056 = 10397056
Example: 1039 & 7056 = 10397056
推荐答案
如果你能找到的情况下,这是够贵的,以引起任何关注,我会很感动
If you can find a situation where this is expensive enough to cause any concern, I'll be very impressed:
int a = 1039;
int b = 7056;
int newNumber = int.Parse(a.ToString() + b.ToString())
或者,如果你希望它是多一点。NET杂交:
Or, if you want it to be a little more ".NET-ish":
int newNumber = Convert.ToInt32(string.Format("{0}{1}", a, b));
int.Parse是的不的一个昂贵的操作。 。花时间担心网络I / O和O的^ N正则表达式
int.Parse is not an expensive operation. Spend your time worrying about network I/O and O^N regexes.
其他说明:实例化的开销的StringBuilder意味着没有点,如果你只是做了几个串联。而且很重要的是 - 如果你的是的规划,把这个回一个整数,记住它限于〜20亿。串联数变得非常大非常快,可能远远超出一个32位int的能力。 (当然签署)。
Other notes: the overhead of instantiating StringBuilder means there's no point if you're only doing a few concatenations. And very importantly - if you are planning to turn this back into an integer, keep in mind it's limited to ~2,000,000,000. Concatenating numbers gets very large very quickly, and possibly well beyond the capacity of a 32-bit int. (signed of course).
这篇关于在C#中串连整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!