我想将数字与字符串分开并将它们分配给新变量。字符串是这样的:
Test +5 anything (1e8+2)
Test2 test1 +4 anything2 (1e8+3/×3)
我使用这个功能:
Function REGULAR(ByVal WhichString As String, _
ByVal Pattern As String, _
ByVal ReplaceWith As String, _
Optional ByVal IsGlobal As Boolean = True, _
Optional ByVal IsCaseSensitive As Boolean = True) As String
'Declaring the object
Dim objRegExp As Object
'Initializing an Instance
Set objRegExp = CreateObject("vbscript.regexp")
'Setting the Properties
objRegExp.Global = IsGlobal
objRegExp.Pattern = Pattern
objRegExp.IgnoreCase = Not IsCaseSensitive
'Execute the Replace Method
RegExpReplace = objRegExp.Replace(WhichString, ReplaceWith)
End Function
首先,我尝试将1(从第一个字符串)分配给变量:
variable1 = Split(string)
variable = REGULAR(variable1(3), "\d", vbNullString, True)
但变量为空。做错了什么(我在互联网上发现了这个功能)?
答案 0 :(得分:1)
如果你有一个由数字和其他字符组成的字符串而只想要数字,请尝试:
Public Function NumPart(Inn) As Long
temp = ""
For i = 1 To Len(Inn)
ch = Mid(Inn, i, 1)
If ch Like "[0-9]" Then temp = temp & ch
Next i
NumPart = CLng(temp)
End Function