字符串的平均,最大,最小,总和? [英] Avg,max,min,sum from string?
问题描述
你好,我得到了一个字符串数组,它们是我自己使用的格式为 H:M:S:MS
Hello i got array of string, and they are durations made by myself in format H:M:S:MS
示例字符串:
0:0:4:410
0:0:1:425
0:0:1:802
0:0:1:509
0:0:1:674
0:0:1:628
0:0:2:76
我怎么在阵列列表中这些项目的总和/平均值/最小值/最大值?
Arraylist名称为 arrayLL
.
Arraylist name is arrayLL
.
我是C#的新手,所以希望有人向我展示如何使用字符串.
I'm new in c# so hope someone will show me how to work with strings.
添加到数组的函数是:
if (Session["DT"].ToString() != "")
{
TimeSpan ts = ((DateTime)Session["DT2"]).Subtract((DateTime)Session["DT"]);
Session["TimeL"] = ts.Hours.ToString() + ":"
+ ts.Minutes.ToString() + ":"
+ ts.Seconds.ToString() + ":"
+ ts.Milliseconds.ToString();
}
推荐答案
假设数字表示小时,分钟,秒和毫秒,则可以尝试以下操作:
Assuming the numbers represent hours, minutes, seconds, and milliseconds you can try the following:
// Empty list you will populate:
List<int> durationsInMilliseconds = new List<int>();
// Loop through your existing data, and calculate all
// durations into milliseconds:
foreach (string word in yourDurationArray)
{
string[] values = s.Split(':');
int hoursAsMilliseconds = Integer.parse(values[0]) * 60 * 60 * 1000;
int minutesAsMilliseconds = Integer.parse(values[1]) * 60 * 1000;
int secondsAsMilliseconds = Integer.parse(values[2]) * 1000;
int sumDurationAsMilliseconds = hoursAsMilliseconds +
minutesAsMilliseconds +
secondsAsMilliseconds +
Integer.parse(values[3]);
durationsInMilliseconds.add(sumDurationAsMilliseconds);
}
现在,您应该具有整数类型( durationsInMilliseconds
)的列表,其中包含可比较的单一格式的数字.这样,您应该能够进行所需的任何计算.
Now you should have a list of type Integer (durationsInMilliseconds
) which contains the numbers in a single comparable format. With this, you should be able to do whichever calculations you need.
(PS:如果您需要与原始输入数据相同格式的结果,则必须添加一个操作,以从MS向后计算小时,分钟和秒.)
这篇关于字符串的平均,最大,最小,总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!