回 帖 发 新 帖 刷新版面

主题:急切求助:检测“p52”中的数字”5“用什么程序?

检测“p52”中的数字”5“,并将其(5)取出,赋值给另一变量。如何编程?即检测字母后的第一个数字,或说串中出现的第一个数字,如何检测到?
谢谢!

回复列表 (共9个回复)

沙发

我有个蛮蠢的办法可以解决

    Dim str As String = "p52"
        Dim str1 As String
        Dim bool As Boolean = False
        Dim i As Integer
        Dim b As Integer
        For i = 0 To str.Length - 1
            str1 = str.Chars(i)
            bool = False
            Try
                b = CType(str1, Integer)
            Catch ex As Exception
                bool = True
            End Try
            If Not bool Then
                MsgBox(b)
            End If
        Next

板凳

检测“p52”中的数字”5“,并将其(5)取出,赋值给另一变量。如何编程?

Dim a As String = "p52"
dim i as integer=a.Substring(a.IndexOf("5"), 1)

检测字母后的第一个数字,或说串中出现的第一个数字,如何检测到?
Dim a As String = "p52"
        Dim i, j As Integer
        For i = 0 To a.Length - 1
            Select Case a.Substring(i)
                Case 0 To 9
                     处理代码
            End Select
        Next

3 楼

函数如下:    
Function getFirstDigit(ByVal str As String)
        Dim Rx As System.Text.RegularExpressions.Regex
        Dim sPattern As String = "\d"
        Rx = New System.Text.RegularExpressions.Regex(sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        Dim Cmatch As System.Text.RegularExpressions.Match = Rx.Match(str)
        Return Cmatch.Value
End Function

调用的话:
        Dim strResult As String
        strResult = getFirstDigit("jkljk233dd")
        MsgBox(strResult)

4 楼

dim i as integer=a.Substring(a.IndexOf("5"), 1)  a.IndexOf("5")  是什么意思?
                           "1"是什么意思?

5 楼

函数如下:    
Function getFirstDigit(ByVal str As String)
        Dim Rx As System.Text.RegularExpressions.Regex
        Dim sPattern As String = "\d"
        Rx = New System.Text.RegularExpressions.Regex(sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        Dim Cmatch As System.Text.RegularExpressions.Match = Rx.Match(str)
        Return Cmatch.Value
End Function

能不能分部解释一下?
谢谢!
”\d“什么意思?
IgnoreCase 什么意思?

6 楼

a.indexof("5")是指a字符串中字符5所在的位置。a.substring(start,length),是指在a字符串中取子串,start代表起始位置,length代表所取的长度。

"\d"是正则表达式,表示匹配数字。
IgnoreCase,表示匹配的时候不区分大小写,这里可以去掉,因为是匹配数字。

7 楼

这个是找指定的数字的,不是查找字母后面的数字的!  
        Dim strF As String = "p52"
        Dim intI As Integer
        Dim strA As String
        intI = InStr(1, strF, "5", CompareMethod.Binary) ''查找5的位置
        If intI <> 0 Then             '判断是不是有5的存在
            strA = Mid(strF, intI, 1)  '把5赋给一个新的变量
            TextBox1.Text = strA       '在TEXTBOX1里面显示出来
        End If

8 楼

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strF As String = "p52"
        Dim strA As String
        Dim Lenth As Integer
        Dim I As Integer
        Dim strStemp As Boolean = False
        Lenth = Len(strF)
        For I = 1 To Lenth - 1
            strA = Mid(strF, I, 1)
            strStemp = Check(strA)
            If strStemp = True Then
                strA = Nothing
                strA = Mid(strF, I + 1, 1) ''把字母后面的第一个数字返回给一个变量
                TextBox1.Text = strA
            End If
        Next
    End Sub

    Function Check(ByVal intA As String) As Boolean  ''此函数的功能判断是否是数字还是字符
        If Asc(intA) < 48 OrElse Asc(intA) > 57 Then
            Return True  '不是数字返回正确
        Else
            Return False '否则返回错误
        End If
    End Function

9 楼

strA = Mid(strF, I, 1)
函数Mid()什么意思?
括号中的1是什么含义?

我来回复

您尚未登录,请登录后再回复。点此登录或注册