例如,150safdsdfsdf123456应该产生150
如果文字是150,那就很简单了。只需使用val。但是,我希望能够阅读150sdfsdfsdafsadfwhatever。
我该怎么做?
答案 0 :(得分:0)
使用正则表达式(正则表达式)从字符串中提取数字
Dim value1 as String
Dim temp As Regex = New Regex("\d+")
Dim match As Match = temp.Match("23333 dsadsa fd")
If match.Success Then
value1 = match.Value
Console.WriteLine(value1)
End If
答案 1 :(得分:0)
Dim input As String = "150safdsdfsdf" ' input string contains alphanumeric characters.
Dim output As String = New String((From c As Char In input Select c Where Char.IsDigit(c)).ToArray())
' output is a string variable, in the RHS select each character from the input string ('c')
'and check whether it is a digit or not using IsDigit Function.
' if yes it is append with the output string
' else it is neglected.
'hence we will get only numbers as oru output
msgbox (output)' output will be 150
答案 2 :(得分:0)
如果您只想要开头的数字而中间或结尾没有任何内容,请使用此
Dim input As String = "150safdsdfsdf150"
Dim output As String = New String(input.TakeWhile(Function(c) IsDigit(c)).ToArray())
Val()
实际上有什么问题?
Dim s = "150.9sdfsdfsdafsadfwhatever654"
Dim d = Val(s)
d将是150.9(它接受小数!)
答案 3 :(得分:0)
要匹配字符串开头的数字,可以使用正则表达式:
Imports System.Text.RegularExpressions
Dim input As String = "150safdsdfsdf123456"
Dim re As New Regex("^\d+")
Dim output As String = re.Match(input).Value
注意^
符号,表示字符串的开头。因此,例如,a150safdsdfsdf123456
将无法匹配。