General discussion

Locked

VB6 Split and Join functions

By sl-campbell ·
Can someone please help me?
I am using the Split and Join functions of VB6 to change a string to an array and then back to a string. A string is used because that is what gets stored in a database table or the registry. I get the string from the Dband loop through the values. This technique is great because you do not have to make alot of fields, or keys, for something simple, that has alot of elements, and, ESP., if the number of elements vary. I considered using a collection but I doubt ifit is just as fast, and it would need more code, and a slower loop in order to break the string apart and add the elements to the collection.

In most cases the functions work fine, meet my needs, and are easy to work with,. The problem arises when I use the functions on a string where the NUMBER of elements change, (an item is removed from the list). Then I cannot use the Join function because an empty element gets added.

(The code example will follow the question as a comment because of size limitation - I wanted to give as much detail as possible)

QUESTION:
Who knows of a way to correct the above problem, or a different, more efficient way of doing this at all?
It MUST be easy to work with AND at least as fast as the way I have done it above. Please note that the list could have many elements in it, so a bunch of variables and loops are useless to me. The solution must run with code only, (no objects like a list box, or whatever.)

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

VB6 Split and Join functions

by sl-campbell In reply to VB6 Split and Join functi ...

'CODE BLOCK START

'The functions use a comma as a delimiter
Const cCOMMA$=","

Dim vList As Variant
Dim iUB As Integer
Dim iLoop As Integer

Dim sTestList As String

sTestList = "A,B,C,D,E"

vList = Split(sTestList,cCOMMA)

iUB = UBound(vList)

For iLoop = 0 To iUB
'For this test, remove the third element
If vList(iLoop) = "C" Then vList(iLoop) = vbNullString
Next I

'======================
'Rebuild the list (cannot use Join here because the commas do not get removed)
'sTestList = Join(vList, cComma)

'CORRECTION CODE
'===============================
'The Join doesn't do what I need it to do.
It would create a string with the extra comma that looks like this:
'sTestList = "A,B,,D,E"

'So I do not use the join ,and am doing it like this, (which makes it all less efficient and not as easy to work with):

'reset the string
sTestList = vbNullString

For iLoop = 0 To iUBIf vList(iLoop) <> vbNullString Then
sTestList = sTestList & gcCOMMA & vHideColLst(iLoop)
End If
Next I

'Remove the first comma if it the first char.
If Left$(sTestList, 1) = gcCOMMAThen sTestList = Right$(sTestList, Len(sTestList) - 1)
'===============================

'CODE BLOCK END

Collapse -

VB6 Split and Join functions

by andrew In reply to VB6 Split and Join functi ...

I just used string concatenation to build up the output list. Here are two functions I wrote: The first accepts and returns string list, managing the variant arrays internally. The second accepts and returns variant arrays.

'string list IO
Function RemoveElementString(sList As String, sRemove) As String
Dim vList As Variant, vItem As Variant
Dim sOutputList As String

vList = Split(sList, ",")
For Each vItem In vList
If vItem <> sRemove Then sOutputList = sOutputList & vItem & ","
Next
RemoveElementString = Left(sOutputList, Len(sOutputList) - 1) 'strip the trailing comma
End Function

'variant array IO
Function RemoveElementVariant(vList As Variant, sRemove As String) As Variant
Dim vItem As Variant
Dim sOutputList As String

For Each vItem In vList
If vItem <> sRemove Then sOutputList = sOutputList & vItem & ","
Next
sOutputList = Left(sOutputList, Len(sOutputList) - 1) 'strip the trailing comma
RemoveElementVariant = Array(sOutputList)
End Function

'here are the calls:

MsgBox RemoveElementString("A,B,C,D,E", "C")
MsgBox Join(RemoveElementVariant(Split("A,B,C,D,E", ","), "C"), ",")
End Sub

Hope this helps

Collapse -

VB6 Split and Join functions

by Shanghai Sam In reply to VB6 Split and Join functi ...

Poster rated this answer

Collapse -

VB6 Split and Join functions

by sl-campbell In reply to VB6 Split and Join functi ...

Comment to Answer #1:
The code sample I supplied was just that: code sample. There are errors in it and it was written out of my head, trying to make it understandable as to where the problem lies.

In all actuality, my code is written very similar to your way with 2 exceptions:
1. I do not use a function, and didn't want to use one in this case, because there is ALOT more code ran in conjunction with this section and I wanted it to be as fast as possible. But then again, I probably should.

2. I didn't use a For/Each here - shame! (I use them regularly - [except on recordset fields] - so I should have know better here - old habbits).

Based on that, I timed everything anyways, based on 10,000 loops, not using the sample program,but my actual code. Of course, the function call made it slower, (25% slower). But, not using a function call, and using the For/Each the code executed 25% faster than the For iLoop - of course!

>>>>>>>> My mistake - your win!
If someone else doesn't come up with something better with-in the next few days then I will close this out, otherwise, if someone does, then I will add 500 points for each answer that is better, (by my judgement), so each will get full credit, but reject all answers that are not as efficient.

I have corrected the TEST code in the next comment in case someone else wants to take a shot at it.

Collapse -

VB6 Split and Join functions

by sl-campbell In reply to VB6 Split and Join functi ...

'CODE BLOCK START

'The functions use a comma as a delimiter
Const cCOMMA$=","

Dim vList As Variant
Dim iUB As Integer
Dim iLoop As Integer

Dim sTestList As String

sTestList = "A,B,C,D,E"

vList = Split(sTestList, cCOMMA)iUB = UBound(vList)

For iLoop = 0 To iUB

'==============================
'For this test, remove the third element
sTestList = vbNullString
If vList(iLoop) <> "C" Then
sTestList = sTestList & cCOMMA & vList(iLoop)
End If
Next iLoop
'==============================

'Remove the last comma if it the last chararacter
If Right$(sTestList, 1) = cCOMMA Then sTestList = Left$(sTestList, Len(sTestList) - 1)

'CODE END

>>>Answer #1 suggests a For/Each in place of the basic For/Next loop - 25% faster. And to use a function in place of the VB Join function.

Any other ideas?

Collapse -

VB6 Split and Join functions

by mark.thomson In reply to VB6 Split and Join functi ...

Use the replace function to replace the pair of delimiters with a single delimiter something like

Replace(sTestList,cComma$ & cComma$,cComma$)

Collapse -

VB6 Split and Join functions

by Shanghai Sam In reply to VB6 Split and Join functi ...

I do not know how many times I have recommended the use of the Replace function to others, and then turn around and forget to use it in places where it comes in handy the most. I guess this sometimes happens to all of us when a problem arises and our minds are so cluttered that we forget to look for simple answers.

For the record:
The For/Each suggested in answer #1 was 25% faster that the basic For/Next Loop. The problem with both is the fact that one needs to loop through ALL items in the list whereas, using the Join function, which is fast C++ code, one can exit the loop as soon as the item is found and set to vbNullString.
But because the Join wasn't used, the advantage of the Join function was lost. A solution was avaiable, but it was much slower.

The Replace function is also fast code, as compared to anything else in VB that would do the same. The advantage of using both functions together improved speed by 43% over the For/Each and 73% faster over the basic For/Next Loop!

Thanks - This shows the value of using tool like TechRepublic in that others can review code and offer a different solution, seeing the problem from a different view, no matter how simple, that might have otherwise taken a long time to"See" a very simple answer, which was standing all along right in front of you.
Thanks for removing the board from my eyes.

Collapse -

VB6 Split and Join functions

by sl-campbell In reply to VB6 Split and Join functi ...

This question was closed by the author

Back to Web Development Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums