[英]What is the best way to convert this string into number of minutes?
I have a combobox in winforms that has the following items: 我在winforms中有一个组合框,其中包含以下项目:
15 min
30 min
1 hr
1 hr 30 min
2 hr
2 hr 30 min
etc . .
Here is the screenshot of the winforms combobox Collection Items editor 以下是winforms combobox Collection Items编辑器的屏幕截图
and i need to parse that string and return an integer which represents the number of minutes. 我需要解析该字符串并返回一个表示分钟数的整数。 I wanted to see the most elegant way of doing that (right now i am splitting by space and then counting array length and it feels a bit wrong. 我想看到最优雅的方式(现在我按空间分割,然后计算数组长度,感觉有点不对劲。
So parsing 所以解析
2h 30 mins
would return 150 将返回150
Since you said this is a combobox , then you will have to parse the value. 既然你说这是一个组合框 ,那么你将不得不解析这个值。 Your user might enter a value of their own as well. 您的用户也可以输入自己的值。
var formats = new[] {"h' hr'", "m' min'", "h' hr 'm' min'"};
TimeSpan ts;
if (!TimeSpan.TryParseExact(value, formats, null, out ts))
{
// raise a validation message to your user.
}
// you said you wanted an integer number of minutes.
var minutes = (int) ts.TotalMinutes;
You can pass any of the strings you showed in your example as the value
. 您可以将示例中显示的任何字符串作为value
传递。
However , be aware that due to how TimeSpan
works, you cannot parse more than 23 hours or more than 59 minutes with this approach. 但是 ,请注意,由于TimeSpan
工作原理,使用此方法无法解析超过23小时或超过59分钟。 Passing "24 hr" or "60 min" or any combination of such will fail. 通过“24小时”或“60分钟”或其任何组合将失败。
I'd use a Dictionary
for this, so there's no parsing at all involved. 我为此使用了一个Dictionary
,因此根本没有解析。 (It works well when there are fixed choices.) I'm more familiar with Delphi's UI controls than .NETs, so there might be a better way to populate the ComboBox
than what I'm doing here, but I'm sure someone will let me know if there is and I can fix it. (当有固定的选择时,它运行良好。)我比熟悉Delphi的UI控件比.NET更熟悉,因此可能有一种更好的方式来填充ComboBox
不是我在这里做的,但我相信有人会让我知道是否有,我可以解决它。
(Code is Oxygene, but it should be easily translatable to C# or VB.Net.) (代码是Oxygene,但它应该很容易转换为C#或VB.Net。)
method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
var
KC: Dictionary<String, Int32>.KeyCollection;
begin
aItems := new Dictionary<String, Int32>;
aItems.Add('15 min', 15);
aItems.Add('30 min', 30);
aItems.Add('1 hr', 60);
aItems.Add('1 hr 30 min', 90);
aItems.Add('2 hr', 120);
aItems.Add('2 hr 30 min', 150);
KC := aItems.Keys;
for s in KC do
comboBox2.Items.Add(s);
comboBox2.DropDownStyle := ComboBoxStyle.DropDownList;
end;
method MainForm.comboBox2_SelectedIndexChanged(sender: System.Object; e: System.EventArgs);
begin
// Safe since style is DropDownList.
label1.Text := aItems[comboBox2.SelectedItem.ToString].ToString();
end;
This should work: 这应该工作:
static int GetAllNumbersFromString(string timeString)
{
int min = 0;
MatchCollection mc=Regex.Matches(timeString, @"\d+");
if(timeString.Contains("hr") && mc.Count = 1)
{
min = mc[0] * 60;
}
else
{
if(mc.Count > 1)
{
min = mc[0] * 60 + mc[1];
}
else
{
min = mc[0];
}
}
return min;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.