我正在使用這兩個函式,因為我想回傳文本字串中的最后一個非數字單詞。文本字串有空格作為數字和文本之間的分隔符。
孤立地,這些功能按預期作業。但是當我把它們結合起來時,我總是得到一個空白的結果。不知道為什么會這樣。
Function ReturnLastWord(The_Text As String)
Dim stGotIt As String
stGotIt = StrReverse(The_Text)
stGotIt = Left(stGotIt, InStr(1, stGotIt, " ", vbTextCompare))
ReturnLastWord = StrReverse(Trim(stGotIt))
End Function
Function RemoveNumbers(Txt As Variant) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "[0-9]"
RemoveNumbers = .Replace(Txt, "")
End With
End Function
sub test
Dim mystring as string
mystring = ReturnLastWord(RemoveNumbers(c_str))
end sub
uj5u.com熱心網友回復:
不是所有數字的最后一個子串
- 這是一種不同的方法。
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Returns the last substring whose characters are not all digits.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetLastWord( _
ByVal Sentence As String, _
Optional ByVal Delimiter As String = " ") _
As String
Dim SplitString() As String: SplitString = Split(Sentence, Delimiter)
Dim S As String
Dim n As Long
For n = UBound(SplitString) To 0 Step -1
S = SplitString(n)
If Len(S) > 0 Then
If Not S Like String(Len(S), "#") Then
GetLastWord = S
Exit Function
End If
End If
Next n
End Function
Sub GetLastWordTEST()
Dim MyString As String
MyString = GetLastWord("234 asd as1 123") ' result: 'as1'
End Sub
uj5u.com熱心網友回復:
當第二個函式替??換一個數字時,它之前的空格保留在字串中。如果要回傳的單詞是最后一個單詞,則字串保留一個空格。使用第一個函式,空格是最后一個字串字符。
為了讓它根據需要回傳,您應該修剪前一個函式中的字串:
Function RemoveNumbers(Txt As Variant) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "[0-9]"
RemoveNumbers = Trim(.Replace(Txt, ""))
End With
End Function
Sub現在,即使最后一個單詞是數字,您的測驗也會正確回傳...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433504.html
