List<int> ids = ExtractIds("United Kingdom (656) - Aberdeen (7707)");
上面的列表应该由下面的方法填充,该方法从括号内删除值。
如果我使用 match.Value 作为字符串并将其分配给 List< string > 它似乎可以正常工作。但是当我尝试将其转换为整数时,我收到错误消息:“输入字符串的格式不正确。”
我究竟做错了什么?
public List<int> ExtractIds(string str)
{
MatchCollection matchCollection = Regex.Matches(str, @"\((.*?)\)");
List<int> ExtractedIds = new List<int>();
foreach (Match match in matchCollection)
{
int theid = int.Parse(match.Value);
ExtractedIds.Add(theid);
}
return ExtractedIds;
}