vb.net如何在字符串中查找一系列数字

时间:2017-12-29 19:47:09

标签: vb.net

我想用一系列数字(0到20)拆分字符串,字母应为.toLower

如何在代码中定义范围?

我试过这样做:("0","1","2","3")

                    Dim Tolerancevalueofext As String = "JS12"
                    Dim removenumber As String = Tolerancevalueofext.Substring(0, Tolerancevalueofext.IndexOf("0","1","2","3")).ToLower

但这绝对是错误的。

1 个答案:

答案 0 :(得分:1)

你的要求我还不清楚,但这是一种方法:
1.仅从字符串中提取数字(使用Regex)。
2.仅从包含数字的字符串中提取字母并将其转换为小写字母。

Private Sub Example()
    Dim Tolerancevalueofext As String = "JS12"

    ' only numbers, output: "12"
    Dim onlynumbers As String = extractNumberFromString(Tolerancevalueofext).ToString()
    ' only characters, output: "js"
    Dim onlycharacters As String = String.Empty
    For Each line As String In Tolerancevalueofext
        If Not (IsNumeric(line)) Then
            onlycharacters += line.ToLower()
        End If
    Next

End Sub

Public Shared Function extractNumberFromString(ByVal value As String) As Integer
    Dim returnVal As String = String.Empty
    Dim collection As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(value, "\d+")

    For Each m As System.Text.RegularExpressions.Match In collection
        returnVal += m.ToString()
    Next

    Return Convert.ToInt32(returnVal)
End Function
  

输出:
onlynumbers =“12”
onlycharacters =“js”