开发者

VB - Append to array?

this is probably a super simple question. But I have this Array that I need to strip useless parts from. But I still want it in an array.

So, the array looks like this when it comes in:

ArrValues(0) "Firstname=FIRSTNAME"
ArrValues(1) "Lastname=LASTNAME"
ArrValues(2) "Username=USERNAME"
ArrValues(3) "Displayname=DISPLAYNAME"

Then I send this array through this code snippet:

For Each s In arrValues
    s = Split(s, "=")
    s = s(1)
Next

This strips the strings so I get only FIRSTNAME and so on. But, I want to send each开发者_如何学编程 cleaned string into an array again. How do I do that?


To write back to the array, you need to use the ArrValues(index) = new_value notation. So, you need to use a regular For loop instead of For Each, and access the array elements by their indexes. Here's how you can do this:

For i = LBound(ArrValues) To UBound(ArrValues)
 s = ArrValues(i)
 s = Split(s, "=")
 ArrValues(i) = s(1)
Next


As long as you have all strings with an '=' sign, this code should work... using MID function. I'm not an performance expert, but I believe that a MID function would have a better performance than the SPLIT (since split will redim each value).

Still, Helen's code might work as well. Just sharing this code to show my MID approach :)

Sub MidArray()

    Dim ArrValues(3) As Variant
    Dim vValue As Variant
    Dim iCount As Integer

    ArrValues(0) = "Firstname=FIRSTNAME"
    ArrValues(1) = "Lastname=LASTNAME"
    ArrValues(2) = "Username=USERNAME"
    ArrValues(3) = "Displayname=DISPLAYNAME"

    For iCount = LBound(ArrValues) To UBound(ArrValues)

        ArrValues(iCount) = Mid(ArrValues(iCount), InStr(ArrValues(iCount), "=") + 1)

    Next iCount

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜