VBA。如何找到字符串中第一个数字的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3547744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA. How to find position of first digit in string
提问by Ololovin
I have string "ololo123". I need get position of first digit - 1. How to set mask of search ?
我有字符串“ololo123”。我需要获取第一个数字的位置 - 1. 如何设置搜索掩码?
回答by Rob
Something like this should do the trick for you:
像这样的事情应该对你有用:
Public Function GetPositionOfFirstNumericCharacter(ByVal s As String) As Integer
For i = 1 To Len(s)
Dim currentCharacter As String
currentCharacter = Mid(s, i, 1)
If IsNumeric(currentCharacter) = True Then
GetPositionOfFirstNumericCharacter = i
Exit Function
End If
Next i
End Function
You can then call it like this:
然后你可以这样称呼它:
Dim iPosition as Integer
iPosition = GetPositionOfFirstNumericCharacter("ololo123")
回答by Spere
Here is a lightweight and fast method that avoids regex/reference additions, thus helping with overhead and transportability should that be an advantage.
这是一种避免正则表达式/引用添加的轻量级和快速方法,因此如果这是一个优势,则有助于开销和可移植性。
Public Function GetNumLoc(xValue As String) As Integer
For GetNumLoc = 1 To Len(xValue)
If Mid(xValue, GetNumLoc, 1) Like "#" Then Exit Function
Next
GetNumLoc = 0
End Function
回答by Edward Leno
Not sure on your environment, but this worked in Excel 2010
不确定您的环境,但这在 Excel 2010 中有效
'Added reference for Microsoft VBScript Regular Expressions 5.5
Const myString As String = "ololo123"
Dim regex As New RegExp
Dim regmatch As MatchCollection
regex.Pattern = "\d"
Set regmatch = regex.Execute(myString)
MsgBox (regmatch.Item(0).FirstIndex) ' Outputs 5
回答by CtrlDot
I actually have that function:
我实际上有这个功能:
Public Function GetNumericPosition(ByVal s As String) As Integer
Dim result As Integer
Dim i As Integer
Dim ii As Integer
result = -1
ii = Len(s)
For i = 1 To ii
If IsNumeric(Mid$(s, i, 1)) Then
result = i
Exit For
End If
Next
GetNumericPosition = result
End Function
回答by dustyburwell
You could try regex, and then you'd have two problems. My VBAfu is not up to snuff, but I'll give it a go:
您可以尝试使用正则表达式,然后您会遇到两个问题。我的 VBAfu 不符合要求,但我会试一试:
Function FirstDigit(strData As String) As Integer
Dim RE As Object REMatches As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Pattern = "[0-9]"
End With
Set REMatches = RE.Execute(strData)
FirstDigit = REMatches(0).FirstIndex
End Function
Then you just call it with FirstDigit("ololo123").
然后你只需用FirstDigit("ololo123").
回答by Oorang
If speed is an issue, this will run a bit faster than Robs (noi Rob):
如果速度是一个问题,这将比 Robs (noi Rob) 运行得快一点:
Public Sub Example()
Const myString As String = "ololo123"
Dim position As Long
position = GetFirstNumeric(myString)
If position > 0 Then
MsgBox "Found numeric at postion " & position & "."
Else
MsgBox "Numeric not found."
End If
End Sub
Public Function GetFirstNumeric(ByVal value As String) As Long
Dim i As Long
Dim bytValue() As Byte
Dim lngRtnVal As Long
bytValue = value
For i = 0 To UBound(bytValue) Step 2
Select Case bytValue(i)
Case vbKey0 To vbKey9
If bytValue(i + 1) = 0 Then
lngRtnVal = (i \ 2) + 1
Exit For
End If
End Select
Next
GetFirstNumeric = lngRtnVal
End Function

