It's Easy!Private Sub Form_Load()
Dim source As String
source = "0000000"
MyWrite source, 1
MyWrite source, 3
MsgBox source
End SubPrivate Function MyWrite(sSrc As String, iPos As Long) As String
Mid(sSrc, iPos) = "1"
MyWrite = sSrc
End Function
Dim source As String
source = "0000000"
MyWrite source, 1
MyWrite source, 3
MsgBox source
End SubPrivate Function MyWrite(sSrc As String, iPos As Long) As String
Mid(sSrc, iPos) = "1"
MyWrite = sSrc
End Function
Dim Str As String
Str = "000000"
WriteBin Str, 1 '执行完后为"100000"
WriteBin Str, 3 '执行完后为"101000"
End SubPrivate Function WriteBin(Source As String, iPos As Long) As String
'
Dim iSel As Long
If iPos < 1 Then
WriteBin = Source
Exit Function
End If
If iPos > Len(Source) Then iPos = Len(Source)
'取第iPos位
iSel = Val(Mid(Source, iPos, 1))
'0,1反转
iSel = 1 - iSel
'重组0,1序列
Source = Mid(Source, 1, iPos - 1) + Trim(Str(iSel)) + Mid(Source, iPos + 1, Len(Source) - iPos)
'返回函数值
WriteBin = Source
End Function不过我还是觉得用二进制的方法更直接。
Dim Str As String
Str = "000000"
WriteBin Str, 1 '执行完后为"100000"
WriteBin Str, 3 '执行完后为"101000"
End SubPrivate Function WriteBin(Source As String, iPos As Long) As String
'
Dim iSel As Long
If iPos < 1 And iPos > Len(Source) Then
WriteBin = Source
Exit Function
End If
'取第iPos位
iSel = Val(Mid(Source, iPos, 1))
'0,1反转
iSel = 1 - iSel
'写回0,1序列
Mid(Source, iPos) = Trim(Str(iSel))
'返回函数值
WriteBin = Source
End Function
source字串,直接在主程序中就可用了
子程序则不必。
VB很可恶