Link to home
Start Free TrialLog in
Avatar of desiredforsome
desiredforsome

asked on

VB6 string replace

So I have an issue that I cannot figure out.

I have a string that contains email addresses

I.E
test@test.com, test@123.com, test@456.com

Open in new window


I have another string set of
 I.E
test@123.com, test@test.com

Open in new window


I want to remove the second string set from string 1. Even though they are not in order.  I don't know how to figure this one out.  I would assume that i need to separate the second string into an array perhaps and do a foreach loop which removes them from string one.

Scratching my head at this moment.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what about:
Dim s1 as String 
Dim s2 as String 
s1 =  "test@test.com, test@123.com, test@456.com"
s2 = "test@123.com, test@test.com"

Dim arr() As String = Split(s2, ",")
Dim i as Long
s1 = replace(s1, " ", "")
i = lbound(arr)
while i <= ubound(arr)
begin
   s1 = replace(s1, arr(i) & ",", "")
   i = i + 1
end

Open in new window

This may help.

Sub Remove()

    Const delimiter As String = ","

    Dim iterStr As Variant
    Dim mainlist As String
    Dim sublist As String
    Dim split_sublist() As String

    mainlist = "test@test.com, test@123.com, test@456.com"
    sublist = "test@123.com, test@test.com"
    
    split_sublist = Split(sublist, delimiter)
    
    For Each iterStr In split_sublist

        'clean iterStr the way you want.
        iterStr = Replace(iterStr, " ", "")

        mainlist = Replace(mainlist, iterStr & delimiter, "")
        mainlist = Replace(mainlist, iterStr, "")

    Next iterStr
    
    'clean mainlist the way you want.
    mainlist = Trim(mainlist)

End Sub

Open in new window

Avatar of desiredforsome
desiredforsome

ASKER

For Hakan, I get a Mismatch on the FOr each line

For Guy -

I dont see where this outputs to a string. Only an array.
Also get unexptected text on Guy.
Can you please write me what error is? I don't have vb6 installed. I try it in excel VBA.
Also please change "Dim iterStr As Variant" to "Dim iterStr" and try.
So the whole error is Error: 10080 (10080) Type Mismatch Line number 17. This is also after I have changed the dimension.

It is on the "For Each" line of code.
Can you please now change it to "Dim iterStr As String" and try?
I get Error 0 Var is already defined as a different type line 17 (that is on the For Each line)
Please try this.

Sub Remove()

    Const delimiter As String = ","

    Dim mainlist As String
    Dim sublist As String
    Dim split_sublist() As String

    mainlist = "test@test.com, test@123.com, test@456.com"
    sublist = "test@123.com, test@test.com"
    
    split_sublist = Split(sublist, delimiter)
    
    For Each iterStr as String In split_sublist

        'clean iterStr the way you want.
        iterStr = Replace(iterStr, " ", "")

        mainlist = Replace(mainlist, iterStr & delimiter, "")
        mainlist = Replace(mainlist, iterStr, "")

    Next iterStr
    
    'clean mainlist the way you want.
    mainlist = Trim(mainlist)

End Sub

Open in new window

Error 0
Expeting 'In' line number 16   (Same line as always). I am try to figure this out with research and and pulling hair out.
ASKER CERTIFIED SOLUTION
Avatar of Hakan Yılmaz
Hakan Yılmaz
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OMG AWESOME!!!!