【问题标题】:Merge Elements Of int array C#合并int数组C#的元素
【发布时间】:2014-10-14 13:53:56
【问题描述】:

我已经在 MSDN 和 Stack overflow 上四处寻找了一段时间,但我似乎找不到任何东西,我想合并一个 int 数组的元素。我查看了 string.join 但似乎找不到 int 的等价物

int[] myArray = new int[] {1,4};
int myArray[0] + myArray[1] would equal 14, Note that i want to merge them together
not mathematically add them.

整个程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exercise2
{

/** Create a reference type called Person.  Populate the Person class with the following properties to store the following information:
First name
Last name
Email address
Date of birth **/ 

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        string DOB = tbDOB.Text;
        DOB.Replace("\\"," ");
        int[] iDOB = DOB.Select(n => Convert.ToInt32(n)).ToArray();
        int day = 

    }


}

}

改成char数组会不会更好?

【问题讨论】:

  • 只是想知道我能做些什么来提高问题的质量?我看到它很快就得到了 2 票反对

标签: c# arrays merge elements


【解决方案1】:

您可以使用string.Concat,然后将结果解析为整数:

int result = int.Parse(string.Concat(myArray));

字符串也是不可变的,您需要将DOB 赋值回去,并且您需要将非数字字符替换为string.Empty 而不是空白,否则您将得到@987654325 @。

DOB = DOB.Replace("\\", "");

【讨论】:

  • 这是否需要我将 myArray 作为字符串开头?
  • 不,你说 myArray 是一个整数数组,如果是,这段代码就可以工作。
猜你喜欢
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2018-12-14
相关资源
最近更新 更多