Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 检查字符串是否包含特定的整数_Vb.net - Fatal编程技术网

Vb.net 检查字符串是否包含特定的整数

Vb.net 检查字符串是否包含特定的整数,vb.net,Vb.net,我有一个网格视图,其中有一列包含字符串(中间列) 在rowDataBound事件中,我想在列中循环查找它包含的整数,然后在第一列中显示一个值 我知道整数的范围是1到63,所以我可以使用FOR循环来循环数字。这是我到目前为止所拥有的 For x As Integer = 1 To 63 If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text Then End If Next 我遇到的问题是使用contains。我不能使

我有一个网格视图,其中有一列包含字符串(中间列)

rowDataBound
事件中,我想在列中循环查找它包含的整数,然后在第一列中显示一个值

我知道整数的范围是1到63,所以我可以使用
FOR循环
来循环数字。这是我到目前为止所拥有的

For x As Integer = 1 To 63

If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text Then

End If

Next
我遇到的问题是使用
contains
。我不能使用以下公式,因为当
x=1
时,数字1、10、11等也是如此

For x As Integer = 1 To 63

If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(x) Then

End If

Next
如何确保每个数字只得到一个结果?i、 e
x=6
将返回UMIS.75OPTR6GROSSMARGIN.F_CV,而不是包含数字6的所有其他字符串

更新-根据一些答案,我可能无法很好地解释这一点。我想在gridview中循环,如果找到了数字1,并且在第二列中只有数字1,而不是10等,那么我想在第一列中显示“run1”。因此,当x=10时,它将显示“运行10”,以此类推

更新2-这是我的解释,抱歉

生成的网格视图如下所示


第二列的顺序未设置且不符合顺序。

在Do INTIL循环中向后走:

Dim bolFoundMatch As Boolean = False
Dim intCursor As Integer = 63

Do Until (bolFoundMatch OrElse intCursor = 0)

     If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(intCursor) Then

          'Anything you want to do when you find your match.

          'This will ensure your loop exits.
          bolFoundMatch = True

     End If 

     intCursor -= 1

Loop

在Do-Until循环中向后走:

Dim bolFoundMatch As Boolean = False
Dim intCursor As Integer = 63

Do Until (bolFoundMatch OrElse intCursor = 0)

     If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(intCursor) Then

          'Anything you want to do when you find your match.

          'This will ensure your loop exits.
          bolFoundMatch = True

     End If 

     intCursor -= 1

Loop
您必须检查标签的整个文本,以确定它是否仅为
1
,而不是
10、11、12、13、

此外,在这种情况下,您应该使用而不是
CType
仅在转换为包含转换运算符的不同类型时使用,这里您总是处理标签

For x As Integer = 1 To 63

    If String.Equals(DirectCast(e.Row.Cells(2).FindControl("lblTagName"), Label).Text, "UMIS.75OPTR" & x & "GROSSMARGIN.F_CV", StringComparison.OrdinalIgnoreCase) Then
        'Do your stuff.
    End If

Next
您必须检查标签的整个文本,以确定它是否仅为
1
,而不是
10、11、12、13、

此外,在这种情况下,您应该使用而不是
CType
仅在转换为包含转换运算符的不同类型时使用,这里您总是处理标签

For x As Integer = 1 To 63

    If String.Equals(DirectCast(e.Row.Cells(2).FindControl("lblTagName"), Label).Text, "UMIS.75OPTR" & x & "GROSSMARGIN.F_CV", StringComparison.OrdinalIgnoreCase) Then
        'Do your stuff.
    End If

Next

你可能会想,如果用另一种方式来做的话。获取字符串中与正则表达式匹配的数字列表

    Dim s As String = "asd12asdasd.sdf3sdf"

    For Each m As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(s, "[\d]*")
        If m.Success AndAlso Not String.IsNullOrEmpty(m.Value) Then
            ' m.Value
        End If
    Next
有了这个数字列表,你可以检查它是否在1到63之间

如果您的字符串具有相同的后缀/前缀,只需删除它们即可显示数字

    Dim s As String = "UMIS.75OPTR12GROSSMARGIN.F_CV"
    Dim number As String = s.Replace("UMIS.75OPTR", "").Replace("GROSSMARGIN.F_CV", "")

你可能会想,如果用另一种方式来做的话。获取字符串中与正则表达式匹配的数字列表

    Dim s As String = "asd12asdasd.sdf3sdf"

    For Each m As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(s, "[\d]*")
        If m.Success AndAlso Not String.IsNullOrEmpty(m.Value) Then
            ' m.Value
        End If
    Next
有了这个数字列表,你可以检查它是否在1到63之间

如果您的字符串具有相同的后缀/前缀,只需删除它们即可显示数字

    Dim s As String = "UMIS.75OPTR12GROSSMARGIN.F_CV"
    Dim number As String = s.Replace("UMIS.75OPTR", "").Replace("GROSSMARGIN.F_CV", "")

当然,如果intcursor=1,那么它将在61处停止,而我想转到1,这就是为什么我将它向后移动。61将在循环到达1之前停止循环。但他希望它从1开始向上,而不是相反方向。哦,完全错过了。它需要穿过整个网格。单独查看我的更新如果intcursor=1,则它将在61处停止,而我想转到1,这就是为什么我将它向后移动。61将在循环到达1之前停止循环。但他希望它从1开始向上,而不是相反方向。哦,完全错过了。它需要穿过整个网格。查看我的更新查看我的更新答案。查看我的更新答案。@Silentbob:没问题,有时很难向其他人解释你想用代码做什么:)。很高兴我能帮忙另外,请注意,因为我使用的是
StringComparison.OrdinalIgnoreCase
,所以匹配不区分大小写。因此,如果标签的文本是例如
umis.75opTr6grOSsmargIN.F_Cv
,只要它们是相同的字符就无所谓了。@Silentbob:没问题,有时很难向其他人解释你想用代码做什么:)。很高兴我能帮忙另外,请注意,因为我使用的是
StringComparison.OrdinalIgnoreCase
,所以匹配不区分大小写。因此,标签的文本是否为例如
umis.75opTr6grOSsmargIN.F_Cv
,只要是相同的字符就无关紧要。对于Regex示例,请记住其字符串以“umis.75”开头。对于Regex示例,请记住其字符串以“umis.75”开头。