vb中利用replace替换,如果一行中有多个相同的数,但是不想全部替换怎么办?

2024-11-07 09:25:56
推荐回答(3个)
回答1:

Replace函数的第五参数表示替换次数(第四参数指替换开始处).

Private Sub Form_Load()
Dim sTemp As String

sTemp = "i am a boy 100 20 100 30"

sTemp = Replace(sTemp, "100", "50", , 1)
sTemp = Replace(sTemp, "100", "60", , 1)

MsgBox sTemp

End Sub

回答2:

Str1 = "i am a boy 100 20 100 30"
strx = "100"
dim stry(2)
stry(0) = 50
stry(1) = 60
for i=1 to ubound(stry)
str1 = replace(Left(Str1, instr(Str1,strx)+len(strx)),strx,stry(i-1)) & right( Str1, len(Str1)-instr(Str1,strx)-len(strx))
next
wscript.echo str1

回答3:

二楼正解

顺便请教第四参数是干嘛用的?