0

目前我正在写:

    string[] ReadText = File.ReadAllLines(_FILE);
    for(int idx = 0; idx < ReadText.Length; idx++)
    {
        if (ReadText[idx].Contains("BANK0")
        || ReadText[idx].Contains("BANK1")
        || ReadText[idx].Contains("BANK2")
        || ReadText[idx].Contains("BANK3")
        || ReadText[idx].Contains("BANK4")
        || ReadText[idx].Contains("BANK5")
        || ReadText[idx].Contains("BANK6")
        || ReadText[idx].Contains("BANK7")
        || ReadText[idx].Contains("BANK8")
        || ReadText[idx].Contains("BANK9")
        || ReadText[idx].Contains("BANK10")
        || ReadText[idx].Contains("BANK11")
        || ReadText[idx].Contains("BANK12")
        || ReadText[idx].Contains("BANK13")
        || ReadText[idx].Contains("BANK14")
        || ReadText[idx].Contains("BANK15")
        || etc.)
    }

如果我想从 BANK0 到 BANK255 进行检查,那就太糟糕了。有没有类似的方法:

    if (ReadText[idx].Contains(string.format("BANK{0}",[0-255]))

提前致谢。

4

2 回答 2

2

Regex能帮你:

//this will match exact the BANKx to BANK255
var isMatch = Regex.Matches(ReadText[idx], "BANK([12]([0-4]\\d|5[0-5])|\\d{1,2})").Count > 0;
//then check for if(isMatch) ...

xxx如果对in没有限制BANKxxx(例如BANK555是 OK),我们可以使用更简单的模式:

var isMatch = Regex.Matches(ReadText[idx], "BANK\\d+").Count > 0;
于 2013-09-12T03:07:04.097 回答
0

像这样的简单功能将起作用:

Private Function strContains(input As String) As Boolean
    If input.Contains("Bank") Then
        Dim temp As Integer = -1
        If Integer.TryParse(input.Substring(4), temp) AndAlso temp > -1 Then
            Return True
        End If
    End If
    Return False
End Function

If strContains(ReadText[idx]) Then

End If
于 2013-09-12T04:12:50.213 回答