Dim   s(3)   As   Byte 
s(0)   =   10 
s(1)   =   20 
s(2)   =   30 
s(3)   =   40 
如上面的代码,如何去掉s(2)?

解决方案 »

  1.   

    Dim   s1(2)   As   Byte 
    For i=0 to 3
    If i<>2 then
    s1(j)=s(i)
    j=j+1
    next
      

  2.   

    Dim s() As BytePrivate Sub Command1_Click()
        Dim s1(2) As Byte
        For i = 0 To 3
        If i <> 2 Then
           s1(j) = s(i)
           j = j + 1
        End If
        Next
        s = s1
        For i = 0 To UBound(s)
           Debug.Print s(i)
        Next
    End SubPrivate Sub Form_Load()
        ReDim s(3)
        s(0) = 10
        s(1) = 20
        s(2) = 30
        s(3) = 40
    End Sub
      

  3.   

    刚才上面的代码只是一个大致的说明,现在关键是s(i)字节数组中的i是从数据库中的图片查询出来的,现在要去掉其中s数组中存在的“0x7F 0xA2”,则转换为 0xDD
      

  4.   

    先复制后调整,比如要去掉第n个元素:
    dim n
    n=2 '去掉第2个元素
    for i=n to ubound(s)-1
        s(i)=s(i+1)
    next
    redim preserve s(ubound(s)-1)
      

  5.   

    试试下面的俺写的代码,可以去除字节数组某个成员或者替换某个成员的值。Private Declare Sub PutMem1 Lib "msvbvm60" (ByVal Ptr As Long, ByVal NewVal As Byte)
    Private Sub Form_Load()
    Dim t(5) As Byte, m() As Byte
    t(0) = 11
    t(1) = 44
    t(2) = 54
    t(3) = 48
    t(4) = 15
    t(5) = 55
    m = GetClearByteIn(t, 3) '从字节数组中去除第三个数据
    For i = 0 To UBound(m)
     Debug.Print m(i) '这里返回新的数据
    Next
    Erase m
    Debug.Print vbCrLf & vbCrLf
    m = GetReplaceByteIn(t, 3, 200) '替换字节数组中第三个为200
    For i = 0 To UBound(m)
     Debug.Print m(i) '这里返回新的数据
    Next
    End SubPublic Function GetClearByteIn(ByRef ByteIn() As Byte, ByVal Number As Long) As Byte()
    Dim i&, Temp() As Byte
     ReDim Temp(UBound(ByteIn) - 1)
      For i = 0 To UBound(ByteIn)
       If i <> Number Then If i > Number Then Temp(i - 1) = ByteIn(i) Else Temp(i) = ByteIn(i)
      Next
    GetClearByteIn = Temp
    End Function
    Public Function GetReplaceByteIn(ByRef ByteIn() As Byte, ByVal Number As Long, ByVal Rep As Byte) As Byte()
    PutMem1 VarPtr(ByteIn(Number)), Rep
    GetReplaceByteIn = ByteIn
    End Function
      

  6.   

    比较赞同江南春的做法对山野的做法有些疑惑1. 使用API有什么优势? 我对这个API不是很了解,但从字面上的理解应该是直接向一个内存地址赋值。 但用它能比VB的下标寻址快多少呢? 同时绕开了VB封装的安全机制是否值得?2. 既然追求性能了,但GetClearByteIn()更让我不解。 其中temp数组先做分配一次内存,然后做从ByteIn做了一次深度拷贝,默认返回数组GetClearByteIn时 分配内存一次,深度拷贝一次,最后函数返回时系统会默认对临时数组temp销毁一次。 当数组比较大时性能的开销会呈几何倍增。
      

  7.   

    我建议你先把字节数组转换成字符串,然后用replace替换,再转回字节数组。
      

  8.   


    Sub s()
        Dim s(3) As Byte, tmp(2) As Byte, i As Byte    s(0) = 10
        s(1) = 20
        s(2) = 30
        s(3) = 40
        
        For i = 0 To UBound(s) - 1
            If i > 1 Then s(i) = s(i + 1)
            tmp(i) = s(i)
        Next
    End Sub
      

  9.   

    不定数量的替换顺序扫描即可
    Option ExplicitSub Main()
        Dim a()     As Byte
        Dim lUBound As Long
        Dim iScan   As Long '扫描的下标'
        Dim iDone   As Long '已完成的下标'
        
        a = NewByteArray(1, 2, 3, 4, &H7F, &HA2, &H7F, 5, &HA2, 6, &H7F)
        PrintArray a
        
        lUBound = UBound(a)
        iDone = -1
        iScan = 0
        While iScan < lUBound
            If (a(iScan) = &H7F) And (a(iScan + 1) = &HA2) Then
                '替换'
                iDone = iDone + 1
                a(iDone) = &HDD
                iScan = iScan + 2
            Else
                '复制'
                iDone = iDone + 1
                If iDone <> iScan Then
                    a(iDone) = a(iScan)
                End If
                iScan = iScan + 1
            End If
        Wend
        If iScan = lUBound Then
            '复制最后1字节'
            iDone = iDone + 1
            If iDone <> iScan Then
                a(iDone) = a(iScan)
            End If
        End If
        '截短数组'
        ReDim Preserve a(iDone)
       
        PrintArray a
        
    End SubFunction NewByteArray(ParamArray Members()) As Byte()
        Dim a()     As Byte
        Dim i       As Long
        
        ReDim a(UBound(Members))
        For i = 0 To UBound(a)
            a(i) = Members(i)
        Next    NewByteArray = a
    End FunctionSub PrintArray(a() As Byte)
        Dim i       As Long
        
        For i = 0 To UBound(a)
            If i > 0 Then Debug.Print "-";
            Debug.Print Right$("0" & Hex(a(i)), 2);
        Next
        Debug.Print
    End Sub