如果items字符串不是整数,我怎样才能对Listbox中的项进行求和?
listBox1.Items.Add("merry has (5 $) ")
listBox1.Items.Add("sandy has (10 $)")
我需要添加这笔钱,输出将在标签中。
答案 0 :(得分:3)
var sum = listBox1.Items.Cast<string>()
.Select(x => int.Parse(Regex.Match(x, @"\d+").Value))
.Sum();
如果您的某些商品不包含任何数字
var sum = listBox1.Items.Cast<string>()
.Select(x => Regex.Match(x, @"\d+").Value)
.Where(x=> !String.IsNullOrEmpty(x))
.Select(x=>int.Parse(x))
.Sum();
答案 1 :(得分:2)
答案 2 :(得分:0)
这是另一种方法,但不像EZI的解决方案那么简洁:
int sum = 0;
foreach (var item in listBox1.Items.Cast<string>())
{
int tmp;
int startInt = item.IndexOf('(');
int endInt = item.IndexOf('$');
if (startInt > -1 &&
endInt > startInt &&
int.TryParse(item.Substring(++startInt, endInt - startInt), out tmp))
{
sum += tmp;
}
}
MessageBox.Show(string.Format("Sum is: {0} $", sum));
// Output:
// Sum is: 15 $
答案 3 :(得分:0)
使用NumberFormatInfo
的自定义decimal.TryParse
的正则表达式的另一种方法:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencySymbol = "$";
nfi.CurrencyPositivePattern = 3;
nfi.CurrencyNegativePattern = 8;
nfi.NegativeSign = "-";
decimal value = 0;
var dollars = from item in listBox1.Items.Cast<string>()
let startIndex = item.IndexOf("(")
where startIndex >= 0
let endIndex = item.IndexOf(")", startIndex + 1)
where endIndex >= 0
let parsable = decimal.TryParse(item.Substring(startIndex + 1, endIndex - startIndex - 1), NumberStyles.Currency, nfi, out value)
where parsable
select value;
decimal sumDollars = dollars.Sum();
0 $n
1 n$
2 $ n
3 n $
答案 4 :(得分:0)
基于DavidP的想法,您可以使用自定义类来存储显示字符串和美元值。
例如,考虑这个简单的类,它将您的字符串作为构造函数的参数,并具有两个属性:DisplayText(string)和DollarValue(int)(并且它会覆盖ToString()方法以在ListBox中显示) 。它会自动从字符串中获取美元金额并相应地设置其DollarValue
属性:
private class CustomListItem
{
public string DisplayText { get; private set; }
public int DollarValue { get; private set; }
public CustomListItem(string text)
{
this.DisplayText = text;
this.DollarValue = GetDollarValueFromString(text);
}
private int GetDollarValueFromString(string input)
{
if (input == null) return 0;
int dollarValue = 0;
int start = input.IndexOf('(');
int end = input.IndexOf('$');
if (start > -1 && end > start)
{
int.TryParse(
input.Substring(++start, end - start),
out dollarValue);
}
return dollarValue;
}
public override string ToString()
{
return DisplayText;
}
}
您可以根据数据源中的字符串创建这些类的列表,并将它们添加到ListBox中,如下所示:
// Normally items would come from some external datasource
var items = new List<string>
{
"merry has (5 $)",
"sandy has (10 $)"
};
// Add our CustomListItem class instead of strings to the list box
items.ForEach(i => listBox1.Items.Add(new CustomListItem(i)));
现在,当您想要显示总和时,您可以使用自定义类的.DollarValue
属性:
int sum = listBox1.Items.Cast<CustomListItem>().Sum(item => item.DollarValue);
MessageBox.Show(string.Format("Sum is: {0} $", sum));
答案 5 :(得分:-3)
我会将美元金额添加为列表框项的值,将整个文本添加为文本。
ListItem li1 = new ListItem();
li1.Text = "merry has (5 $)";
li1.Value = "5";
listBox1.Items.Add(li1);
ListItem li2 = new ListItem();
li2.Text = "sandy has (10 $)"
li2.Value = "10";
listBox1.Items.Add(li2);
然后你可以很容易地将它们加在一起:
double sumMoney = 0;
foreach (ListItem li in listBox1.Items)
{
sumMoney = sumMoney + Convert.ToDouble(li.Value);
}
lblText.Text = sumMoney.ToString()