代码之家  ›  专栏  ›  技术社区  ›  madlan

测试特定字符的字符串VB.NET

  •  1
  • madlan  · 技术社区  · 14 年前

    我有一个具有FTP权限的字符串-“LRSDCWAN”如果字符串包含相关字符,有没有更有效的方法来检查relevant复选框?

            If reader.Item("home_perm").Contains("L") Then
                CBoxList.Checked = True
            End If
            If reader.Item("home_perm").Contains("R") Then
                CBoxRead.Checked = True
            End If
            If reader.Item("home_perm").Contains("S") Then
                CBoxSubDir.Checked = True
            End If
            If reader.Item("home_perm").Contains("D") Then
                CBoxDelete.Checked = True
            End If
            If reader.Item("home_perm").Contains("C") Then
                CBoxCreate.Checked = True
            End If
            If reader.Item("home_perm").Contains("W") Then
                CBoxWrite.Checked = True
            End If
            If reader.Item("home_perm").Contains("A") Then
                CBoxAppend.Checked = True
            End If
            If reader.Item("home_perm").Contains("N") Then
                CBoxRename.Checked = True
            End If
    

    谢谢。

    5 回复  |  直到 14 年前
        1
  •  5
  •   Dan Joseph    14 年前

    虽然它不能解决.Contains()问题,但可以简化逻辑。

    如果您注意到,您正在使用:

    If reader.Item("home_perm").Contains("L") Then
        CBoxList.Checked = True
    End If
    

    你可以简单地说

    CBoxList.Checked = reader.Item("home_perm").Contains("L")
    

        2
  •  2
  •   Jon Skeet    14 年前

    Dictionary(Of Char, CheckBox)

    ' Assumes VB10 collection initializers
    Dim map As New Dictionary(Of Char, CheckBox) From {
        { "L", CBoxList },
        { "R", CBoxRead },
        { "S", CBoxSubDir },
        { "D", CBoxDelete },
        { "C", CBoxCreate }
    }
    
    For Each c As Char In reader.Item("home_perm")
        Dim cb As CheckBox
        If map.TryGetValue(c, cb) Then
            cb.Checked = True
        End If
    Next
    
        3
  •  2
  •   MaQleod    14 年前

    http://msdn.microsoft.com/en-us/library/hs600312(VS.71).aspx

    string.indexof方法:

        Dim myString As String = "LRSDCW" 
        Dim myInteger As Integer 
        myInteger = myString.IndexOf("D") // myInteger = 4 
        myInteger = myString.IndexOf("N") // myInteger = -1
    

    对myInteger使用一个数组,并检查数组中的每个成员是否有-1以外的值,如果是-1,则不要选中该框。

        4
  •  0
  •   Phil Helix    14 年前

    你能把所有的比较值放在一个通用的列表中,然后遍历这个列表吗?

        5
  •  0
  •   Tom W    14 年前
    CBoxRename.Checked = (New Regex("[LRSDCWAN]").Match(reader.Item("home_perm")).Count > 0) ?