C# 拆分数字和字符的字符串

C# 拆分数字和字符的字符串,c#,regex,string,split,C#,Regex,String,Split,例如,我有一个包含字母数字和字母字符串的列表 List<string> liRoom = new List<string>() {"Room1","Room2","Room3", "Room4","Hall","Room5","Assembly", "Room6","Room7","Room8","Ro

例如,我有一个包含字母数字和字母字符串的列表

List<string> liRoom = new List<string>() {"Room1","Room2","Room3",  
                                         "Room4","Hall","Room5","Assembly",  
                                         "Room6","Room7","Room8","Room9};
List liRoom=new List(){“Room1”、“Room2”、“Room3”,
“4号房间”、“大厅”、“5号房间”、“集会”,
“6号房间”、“7号房间”、“8号房间”、“9号房间”;
此列表的类型为字母数字和字母,因此我想从该字符串列表中获取最大数值。
我曾经试过这样做

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.
var ss=newregex((?[a-zA-Z]+)(?[0-9]+);
List liNumeric=新列表();
foreach(房间内的串st)
{   
var varMatch=ss.Match(st);
添加(Convert.ToInt16(varMatch.Groups[“Numeric”].Value));
}  
int MaxValue=liNumeric.Max();//上述示例中的结果必须是9。

List liNumeric=new List();
foreach(房间内的串st)
{   
Add(int.Parse(新字符串(st.Where(char.IsDigit.ToArray()));
}  
int MaxValue=liNumeric.Max();//上述示例中的结果必须是9。
但当
st
Hall,Assembly


请帮助我如何执行此操作。

代码中出现异常的原因很少。我为这些可能的异常添加了一些条件

List<int> liNumeric = new List<int>();  
 foreach (string st in liRoom)  
 { 
   // int.Parse will fail if you don't have any digit in the input 
   if(st.Any(char.IsDigit))
   {
       liNumeric.Add(int.Parse(new string(st.Where(char.IsDigit).ToArray()))); 
   }

 }  
 if (liNumeric.Any()) //Max will fail if you don't have items in the liNumeric
 {
     int MaxValue = liNumeric.Max();
 }
List liNumeric=new List();
foreach(房间内的串st)
{ 
//如果输入中没有任何数字,解析将失败
如果(任何(字符为数字))
{
Add(int.Parse(新字符串(st.Where(char.IsDigit.ToArray()));
}
}  
if(liNumeric.Any())//如果liNumeric中没有项,Max将失败
{
int MaxValue=liNumeric.Max();
}

请尝试以下操作:

List<string> liRoom = new List<string>() {"Room1","Room2","Room3",  
                                         "Room4","Hall","Room5","Assembly",  
                                         "Room6","Room7","Room8","Room9"};


var re = new Regex(@"\d+");

int max = liRoom.Select(_ => re.Match(_))
                .Where(_ => _.Success)
                .Max( _ => int.Parse(_.Value));

/* 
   max = 9 
*/
List liRoom=new List(){“Room1”、“Room2”、“Room3”,
“4号房间”、“大厅”、“5号房间”、“集会”,
“6号房间”、“7号房间”、“8号房间”、“9号房间”};
var re=new Regex(@“\d+”);
int max=liRoom.Select(=>re.Match())
.Where(=>u.Success)
.Max(=>int.Parse(0.Value));
/* 
最大值=9
*/

您应该通过检查匹配是否成功,在代码中添加以下内容

if (varMatch.Success)
{
     liNumeric.Add(Convert.ToInt16(varMatch.Groups["Numeric"].Value));
}

您不需要foreach,只需一条语句即可:

int value = liRoom.Where(x => x.Any(char.IsDigit))
            .Select(x => Convert.ToInt32(new String(x.Where(char.IsDigit).ToArray())))
            .Max();

这看起来很奇怪,但很有效。:)

不错,但Damith很快。为什么我不喜欢lambda表达式中的下划线“\u1”?
int value = liRoom.Where(x => x.Any(char.IsDigit))
            .Select(x => Convert.ToInt32(new String(x.Where(char.IsDigit).ToArray())))
            .Max();