【发布时间】:2014-02-11 03:17:29
【问题描述】:
我有一个列表 liRoom,其中包含一个字母数字和字母字符串例如
List<string> liRoom = new List<string>() {"Room1","Room2","Room3",
"Room4","Hall","Room5","Assembly",
"Room6","Room7","Room8","Room9};
这个列表是字母数字和字母类型,所以我想从这个字符串列表中获取最大数值。
我曾尝试过这样做
var ss = new Regex("(?<Alpha>[a-zA-Z]+)(?<Numeric>[0-9]+)");
List<int> liNumeric = new List<int>();
foreach (string st in liRoom)
{
var varMatch = ss.Match(st);
liNumeric.Add(Convert.ToInt16(varMatch.Groups["Numeric"].Value));
}
int MaxValue = liNumeric.Max();// Result Must be 9 from above Example.
还有
List<int> liNumeric = new List<int>();
foreach (string st in liRoom)
{
liNumeric.Add( int.Parse(new string(st.Where(char.IsDigit).ToArray())));
}
int MaxValue = liNumeric.Max();// Result Must be 9 from above Example.
但是当st 是Hall,Assembly 时两者都显示错误
帮帮我怎么做。
【问题讨论】: