在 C# 中连接整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1014292/
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 in C#
提问by CountCet
Is there an inexpensiveway to concatenate integers in csharp?
在 csharp 中是否有一种廉价的方法来连接整数?
Example: 1039 & 7056 = 10397056
示例:1039 & 7056 = 10397056
采纳答案by Rex M
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())
Or, if you want it to be a little more ".NET-ish":
或者,如果您希望它更“.NET-ish”:
int newNumber = Convert.ToInt32(string.Format("{0}{1}", a, b));
int.Parse is notan expensive operation. Spend your time worrying about network I/O and O^N regexes.
int.Parse不是一个昂贵的操作。花时间担心网络 I/O 和 O^N 正则表达式。
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 areplanning 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).
其他注意事项:实例化 StringBuilder 的开销意味着如果您只进行一些连接,则没有意义。而且很重要的是-如果你是打算把这个回整,记住它的限制为约20亿。连接数字非常快地变得非常大,并且可能远远超出 32 位 int 的容量。(当然签名)。
回答by Tim Hoolihan
string ConcatInt(int x,int y){return String.Format("{0}{1}",x,y);}
int ConcatInt(int x,int y){ return (x * Math.Pow(10, y.length)) + y; }
string ConcatInt(int x,int y){return String.Format("{0}{1}",x,y);}
int ConcatInt(int x,int y){ return (x * Math.Pow(10, y.length)) + y; }
Edit Note: Fixes some mistypes. There are more type issues left. I'm just giving an outline of the answer
编辑注意:修复了一些错误类型。还有更多类型问题。我只是给出答案的大纲
The second method should actually be:
第二种方法实际上应该是:
static int ConcatInt2(int x, int y) {
return (int)(x * Math.Pow(10, y.ToString().Length)) + y;
}
回答by Cade Roux
If we want integer result then:
如果我们想要整数结果,那么:
int result = int.Parse(input1.ToString() + input2.ToString());
For a string result do this:
对于字符串结果,请执行以下操作:
string result = input1.ToString() + input2.ToString();
回答by C. Ross
The "Mathy" and "No String" method follows:
“Mathy”和“No String”方法如下:
int a = 1039;
int b = 7056;
int bLen = (int)Math.Ceiling(Math.Log10(b));
int ab = (a * ((int)Math.Pow(10, bLen))) + b;
Note that it may still be slow because of the Log10 call.
请注意,由于 Log10 调用,它可能仍然很慢。
回答by whatsisname
inexpensive? String concatenation or formatted string is probably going to be considerably faster.
便宜?字符串连接或格式化字符串可能会快得多。
Otherwise you can do something like:
否则,您可以执行以下操作:
Math.Pow(10,Math.Ceiling(Math.Log10(second)))*first+second
provided first and second are integers. This is about the only way you'll do it not involving converting to a string and back, but I am extremely doubtful that it will be faster.
如果第一个和第二个是整数。这是您不涉及转换为字符串并返回的唯一方法,但我非常怀疑它会更快。
回答by jitter
If you want to concatenate many ints to a String
如果您想将多个整数连接到一个字符串
StringBuilder sb = new StringBuilder(1039);
sb.Append(7056);
sb.Append(1234);
sb.Append(1235);
....
sb.Append(9999);
sb.ToString();
回答by Michael Stum
Not really inpexpensive, but:
不是很便宜,但是:
string con = string.Format("{0}{1}",int1,int2);
or
或者
string con = int1.ToString() + int2.ToString();
If you use this in a loop, I think I would use Option 1, which uses a StringBuilder internally.
如果您在循环中使用它,我想我会使用选项 1,它在内部使用 StringBuilder。
回答by Jamie Ide
public int ConcatInts(int int1, int int2)
{
return (int)(int1 * Math.Pow(10, int2.ToString().Length)) + int2;
}
Edit: Guess I wasn't the first with this solution!
编辑:猜猜我不是第一个使用此解决方案的人!
回答by Skizz
I don't think you can get any simpler than this:
我认为没有比这更简单的了:
static uint Concat (uint a, uint b)
{
uint
pow = 1;
while (pow < b)
{
pow = ((pow << 2) + pow) << 1;
a = ((a << 2) + a) << 1;
}
return a + b;
}
which has no memory allocations, string conversions or multiplies; or maybe:
它没有内存分配、字符串转换或乘法;或者可能:
static uint Concat (uint a, uint b)
{
uint
pow = 1;
while (pow < b)
{
pow = ((pow << 2) + pow) << 1;
}
return a * pow + b;
}
If you want to concatenate two binary numbers:
如果要连接两个二进制数:
static uint Concat (uint a, uint b)
{
uint
mask = uint.MaxValue;
while ((mask & b) != 0)
{
mask <<= 1;
a <<= 1;
}
return a | b;
}
回答by Jimmy
how about this?
这个怎么样?
int c = b;
while(c > 0) {
a *= 10;
c /= 10;
}
a += b;