我有一個字符串,可以輸入爲{n}d {n}h {n}m {n}s
,其中{n}
是一個整數,表示天數,小時數,分鐘數,秒數。我如何從字符串中提取這個{n}
號碼?解析字符串的整數部分
用戶不必輸入全部4-d,h,m,s。他只能輸入4d,即4天或5h 2s,即5小時2秒。
這是我的。肯定應該有更好的方法來做到這一點。此外,並不涵蓋所有情況。
int d; int m; int h; int sec;
string [] split = textBox3.Text.Split(new Char [] {' ', ','});
List<string> myCollection = new List<string>();
foreach (string s in split)
{
d = Convert.ToInt32(s.Substring(0,s.Length-1));
h = Convert.ToInt32(split[1].Substring(1));
m = Convert.ToInt32(split[2].Substring(1));
sec = Convert.ToInt32(split[3].Substring(1));
}
dt =new TimeSpan(h,m,s);
指定'RegexOptions.ExplicitCapture'而不是使用非捕獲組,並且你很好... – Lucero
@Lucero:哇,我學到了一些東西新! –