我有字符串「ololo123」。 我需要得到第一位的位置 - 1. 如何設置搜索掩碼?VBA。如何找到字符串中第一個數字的位置
11
A
回答
8
像這樣的東西應該爲你做的伎倆:
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
然後你可以這樣調用:
Dim iPosition as Integer
iPosition = GetPositionOfFirstNumericCharacter("ololo123")
0
如果速度是一個問題,這將運行略高於剝奪快(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
1
你可以嘗試正則表達式,然後你會遇到兩個問題。我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
然後你只用調用它。
+0
+1我看到我們有類似的想法,你快一點! – 2010-08-23 14:07:42
2
不知道你的環境,但這個工作在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
2
我其實有一個功能:
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
7
下面是避免了正則表達式/參考添加一個輕量級的和快速的方法,從而有助於開銷和運輸能力,如果這是一個優勢。
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
相關問題
- 1. Scheme:如何找到一個字符串中的字符位置
- 2. 查找字符串的第一個字符的位置
- 3. 查找字符串中第一個字母的位置
- 4. Python找到字符串中最後一個數字的位置
- 5. 如何找到一個字符的位置在一個字符串中PHP
- 6. 如何找到在C中第一個字符的字符串++
- 7. 如何獲取字符串中第一個字母的位置
- 8. 如何找到字符串放在字符串中的位置?
- 9. 找到一個字符在字符串中的位置?
- 10. 找到兩個字符串之間不同的第一個字符位置
- 11. 如何查找字符串中第五個空格的位置?
- 12. 如何查找字符串中第三個「_」的位置 - JavaScript
- 13. 如何找到字符串中的字位置(不是字符位置)
- 14. Python。如何編寫一個函數來查找字符串中第一次出現子字符串的位置?
- 15. PHP字符串如何找到一串數字一個數字
- 16. 字符串中的第一個找到的數字與jQuery的子字符串
- 17. 如何找到字符串中特定字符的位置
- 18. 如何找到字符串數組中特定字符串的位置?
- 19. 查找BigQuery中另一個字符串中的一個字符串的位置
- 20. 找到一個字符串的位置在另一個字符串
- 21. 查找字符串中第一次出現子串的位置
- 22. 查找字符串中最後一位數字的位置
- 23. Python:如何找到字符串的第一個字母?
- 24. 如何從字符串中找到最後一個字母表的位置?
- 25. 如何在字符串中找到第n個字符?
- 26. 如何找到一個字符串中的字符串
- 27. 如何使用Excel公式找到Excel字符串中最後一個字母數字字符的位置?
- 28. 如何找到字符串位數
- 29. 如何在Python中的字符串中找到一個數字?
- 30. 如何將連字符添加到每個第n個位置的字符串?
@Remou謝謝,我刪除了這句話。 – Fosco 2010-08-23 13:26:48