VB Replace函数
本帖最后由 hhxngz 于 2012-11-18 21:02:22 编辑
大家好,有个关于VB Replace函数的问题:
目的是将字符串
"X=-700-80*cos(45) Y50*sin(30)Z=-973+60"换为:
"X=-700-80*cos(45) Y50*sin[30]Z=-973+60"
为什么结果是:"sin[30]Z=-973+60",前面一段字符串给丢了!
Private Sub Command1_Click()
Dim strTemp As String
Dim strN As String
Dim intSearch As String
strTemp = "X=-700-80*cos(45) Y50*sin(30)Z=-973+60"
If InStr(1, UCase(strTemp), UCase("SIN(")) Then
strN = Replace(UCase(strTemp), UCase("SIN("), "SIN[", , 1)
End If
intSearch = 0
intSearch = InStr(1, strN, "SIN")
strN = Replace(UCase(strN), UCase(")"), "]", intSearch)
MsgBox strN
End Sub
[解决办法]
给你做个函数吧:
Private Sub Command1_Click()
Dim st As String
st = "X=-700*sin(30)-80*cos(45) Y50*sin(30)Z=-973+60"
Text1 = ReplaceSIN(st)
End Sub
Private Function ReplaceSIN(ByVal st As String) As String
Dim i As Long
Do While InStr(i + 1, LCase(st), "sin(") > 0
i = InStr(i + 1, LCase(st), "sin(")
ReplaceSIN = ReplaceSIN & Left(st, i + 2) & "[" & Mid(st, i + 4, InStr(i, st, ")") - i - 4) & "]"
st = Mid(st, InStr(i, st, ")") + 1)
Loop
ReplaceSIN = ReplaceSIN & st
End Function
'*****结果
'X=-700*sin[30]-80*cos(45) Y50*sin[30]Z=-973+60
'*********