C#试图比较文本框中的行,并计算具有不同数量的行(有时是重复的)的数量
本文关键字:计算 文本 比较 | 更新日期: 2023-09-27 18:25:09
我还是编程新手,一直在为工作中的同事创建一些工具来完成一些多余的任务。正如我在标题中所说,我想做这样的事情:
文本框1可能具有:
苹果3
苹果4
橙子1
我想把这个转移到文本框2,如下所示:
苹果7
橙子1
它考虑到苹果出现了两次,并将总数相加。到目前为止,我的代码enter code here
不起作用,我已经为此挣扎了一段时间:
{
foreach (var lines in textBox1.Lines)
{
string line = lines.TrimEnd();
if (line.Length > 0)
{
int findIndex = line.LastIndexOf(" ") + 1;
var qty = (line.Substring(findIndex));
int lineInt;
try
{
lineInt = Convert.ToInt32(qty); //convert whatever the qty detected was, to int32
{
textBox2.Text += line + lineInt+Environment.NewLine.ToString();
}
}
catch (Exception)
{
}
}
}
}
结果是:
苹果33
苹果44
Oranges 11
这是错误的,因为我没有删除末尾的初始数量字段,也没有正确地将整数值加在末尾,我只是重新附加了末尾的数量。我的最终目标是列出许多不同的产品/数量,并将它们相加,通过设置差异/增量或类似的操作来查看与以前的订单相比可能发生了哪些数量的变化,在将正确的数据输入文本框2之前,我无法做到这一点。对我来说,棘手的部分是合并重复的产品线,但仍然将它们的总数量相加。任何见解都将不胜感激,感谢阅读!
试试这个
ASPX-
<asp:Panel ID=pnl1 runat=server>
<asp:TextBox ID=TextBox1 runat=server></asp:TextBox>
<asp:TextBox ID=TextBox2 runat=server></asp:TextBox>
<asp:TextBox ID=TextBox3 runat=server></asp:TextBox>
<asp:TextBox ID=TextBox4 runat=server></asp:TextBox>
<asp:TextBox ID=TextBox5 runat=server></asp:TextBox>
<asp:TextBox ID=TextBox6 runat=server></asp:TextBox>
</asp:Panel>
<br />
Total
<asp:Panel ID=Panel1 runat=server>
</asp:Panel>
<asp:Button ID=btn runat=server Text=Total onclick="btn_Click" />
CS。侧面
protected void btn_Click(object sender, EventArgs e)
{
Dictionary<string, int> obj = new Dictionary<string, int>();
foreach (Control cn in pnl1.Controls)
{
if (cn.GetType() == typeof(TextBox))
{
if (((TextBox)cn).Text.Trim() != "")
{
string[] spValue = ((TextBox)cn).Text.Split(' ');
if (obj.ContainsKey(spValue[0]))
{
obj[spValue[0]] = obj[spValue[0]] + Convert.ToInt32(spValue[1]);
}
else
{
obj.Add(spValue[0], Convert.ToInt32(spValue[1]));
}
}
}
}
foreach (string k in obj.Keys)
{
TextBox tx = new TextBox();
tx.Text = k + " " + obj[k].ToString();
pnl1.Controls.Add(tx);
}
}
string previousLine = "";
foreach (var lines in textBox1.Lines)
{
string line = lines.TrimEnd();
if (line.Length > 0 && previousLine != "")
{
int findIndex = line.LastIndexOf(" ") + 1;
//check to ensure the item in this line is equal to the item in the previous line
if(line.substring(0, findIndex - 2) == previousLine.substring(0, findIndex - 2))
{
var qty = (line.Substring(findIndex));
int lineInt;
try
{
lineInt = Convert.ToInt32(qty); //convert whatever the qty detected was, to int32
{
int previousStringNum = Int32.TryParse(previousLine.Substring(previousLine.LastIndexOf(" ") + 1)
int sumOfLineAndPreviousLine = lineInt + previousStringNum;
//set the line variable equal to its new value which includes the value from the previous line
line = line.substring(0, line.length - 2) + sumOfLineAndPreviousLine.ToString();
//textBox2.Text += line + lineInt+Environment.NewLine.ToString();
//set the previousLine variable equal to line so that the next pass of the foreach loop if that object equals the object in previousLine, it will continue to add them...
previousLine = line;
}
}
catch (Exception)
{
}
}
else
{
//the object in this line is not equal to the object in the previousLine
//so append the previousLine to the TextBox2.Text and then
//set the previousLine object equal to the current Line so that on the
//next pass of the loop will work correctly
textBox2.Text = textBox2.Text + previousLine;
previousLine = line;
}
}
}
我相信这可能会达到你想要的,尽管它是相当有限的。这假设所有对象都只有一个数字。需要更改代码以处理两位数等。