【发布时间】:2015-01-27 13:43:44
【问题描述】:
我需要对字符串变量中的几个值进行求和,
这是我的变量:
string strBillHeader = "Invoice Details
INVOICE_DATE,INVOICE_DESCRIPTION,VALUE,FROM_DATE,TO_DATE
01/11/2014,New Corpbundle 7,7,01/11/2014,30/11/2014
01/11/2014,New Corpbundle 7,-7,01/11/2014,30/11/2014
01/11/2014,New Corpbundle 7,7,01/11/2014,30/11/2014
01/11/2014,Missed Call ALERT with Offer,2,01/11/2014,30/11/2014"
在这种情况下,我需要取出 (7,-7,7,2) 的值吗?结果得到 9。
我试着这样做:
for (int x = 4; x <= countCommas - 3; x += 4)
{
int firstCommaIndex = strBillHeader.IndexOf(',', strBillHeader.IndexOf(',') + x);
int secondCommaIndex = strBillHeader.IndexOf(',', strBillHeader.IndexOf(',') + (x + 1));
string y = strBillHeader.Substring(firstCommaIndex + 1, 1);
chargeAmount = Convert.ToInt32(y);
//chargeAmount = Int32.Parse(strBillHeader.Substring(firstCommaIndex, secondCommaIndex - firstCommaIndex));
TotalChargeAmount += ChargeAmount;
//string AstrBillHeader = strBillHeader.Split(',')[0];
}
但它不起作用,因为我在 y 变量中不断得到“V”。
任何帮助将不胜感激
【问题讨论】:
-
使用 string.Split 和 Linq 表达式来总结你的行的第二列。
-
为什么不想要最后一行的 2?
-
我想要它,但我忘记在问题中添加它......对不起
-
该字符串中是否有换行符或者它只是一行文本?
-
有趣,请检查你的数学。