C#中的连接整数

时间:2009-06-18 18:11:11

标签: c#

是否有便宜的方法在csharp中连接整数?

示例:1039& 7056 = 10397056

12 个答案:

答案 0 :(得分:56)

如果你能找到一个价格昂贵而引起任何担忧的情况,我会非常感动:

int a = 1039;
int b = 7056;

int newNumber = int.Parse(a.ToString() + b.ToString())

或者,如果你想让它更像“.NET-ish”:

int newNumber = Convert.ToInt32(string.Format("{0}{1}", a, b));

int.Parse 是一项昂贵的操作。花点时间担心网络I / O和O ^ N正则表达式。

其他说明:实例化StringBuilder的开销意味着如果你只进行几次连接就没有意义了。而且非常重要的是 - 如果 计划将其重新转换为整数,请记住它仅限于2,000,000,000。连接数非常快,并且可能远远超出32位int的容量。 (当然签名)。

答案 1 :(得分:11)

我在派对上有点晚了,但最近我不得不连接整数。 0 <0 a,b&lt; 10 ^ 9它可以很快完成。

static ulong concat(uint a, uint b)
{
    if (b < 10U) return 10UL * a + b;
    if (b < 100U) return 100UL * a + b;
    if (b < 1000U) return 1000UL * a + b;
    if (b < 10000U) return 10000UL * a + b;
    if (b < 100000U) return 100000UL * a + b;
    if (b < 1000000U) return 1000000UL * a + b;
    if (b < 10000000U) return 10000000UL * a + b;
    if (b < 100000000U) return 100000000UL * a + b;
    return 1000000000UL * a + b;
}

编辑:下面的版本可能很有趣(平台目标:x64)。

static ulong concat(ulong a, uint b)
{
    const uint c0 = 10, c1 = 100, c2 = 1000, c3 = 10000, c4 = 100000,
        c5 = 1000000, c6 = 10000000, c7 = 100000000, c8 = 1000000000;
    a *= b < c0 ? c0 : b < c1 ? c1 : b < c2 ? c2 : b < c3 ? c3 :
         b < c4 ? c4 : b < c5 ? c5 : b < c6 ? c6 : b < c7 ? c7 : c8;
    return a + b;
}

答案 2 :(得分:8)

  1. string ConcatInt(int x,int y){return String.Format("{0}{1}",x,y);}
    
  2. int ConcatInt(int x,int y){
       return (x * Math.Pow(10, y.length)) + y;
    }
    
  3. 编辑注意:修复了一些错误类型。还剩下更多的类型问题。我只是简要回答一下

    第二种方法实际应该是:

    static int ConcatInt2(int x, int y) {
       return (int)(x * Math.Pow(10, y.ToString().Length)) + y;
    }
    

答案 3 :(得分:8)

便宜的?字符串连接或格式化字符串可能要快得多。

否则你可以这样做:

Math.Pow(10,Math.Ceiling(Math.Log10(second)))*first+second

提供第一个和第二个是整数。这是你要做的唯一方法,不涉及转换为字符串和返回,但我非常怀疑它会更快。

答案 4 :(得分:8)

我认为你不能比这更简单:

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;
}

没有内存分配,字符串转换或乘法;或者可能:

static uint Concat (uint a, uint b)
{
  uint
    pow = 1;

  while (pow < b)
  {
    pow = ((pow << 2) + pow) << 1;
  }

  return a * pow + b;
}

如果要连接两个二进制数:

static uint Concat (uint a, uint b)
{
  uint
    mask = uint.MaxValue;

  while ((mask & b) != 0)
  {
    mask <<= 1;
    a <<= 1;
  }

  return a | b;
}

答案 5 :(得分:6)

如果要将多个int连接到String

StringBuilder sb = new StringBuilder(1039);
sb.Append(7056);
sb.Append(1234);
sb.Append(1235);
....
sb.Append(9999);
sb.ToString();

答案 6 :(得分:3)

如果我们想要整数结果,那么:

int result = int.Parse(input1.ToString() + input2.ToString());

对于字符串结果,请执行以下操作:

string result = input1.ToString() + input2.ToString();

答案 7 :(得分:2)

“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;

请注意,由于Log10调用,它可能仍然很慢。

答案 8 :(得分:2)

怎么样?

int c = b;
while(c > 0) {
   a *= 10;
   c /= 10;
}
a += b;

答案 9 :(得分:1)

不是很贵,但是:

string con = string.Format("{0}{1}",int1,int2);

string con = int1.ToString() + int2.ToString();

如果你在循环中使用它,我想我会使用Option 1,它在内部使用StringBuilder。

答案 10 :(得分:1)

public int ConcatInts(int int1, int int2)
{
    return (int)(int1 * Math.Pow(10, int2.ToString().Length)) + int2;
}

编辑:猜猜我不是第一个使用这个解决方案的人!

答案 11 :(得分:0)

//连接两个数字程序//

        Console.WriteLine("Enter a number for a");
        int a = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a number for b");
        int b = int.Parse(Console.ReadLine());

        int Concatenating = Convert.ToInt32(string.Format("{0}{1}", a, b));
        Console.WriteLine(Concatenating);
        Console.ReadKey();