Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

VB help, trying to ad spaces


elmiguel's Avatar
Member
2,795 1

Hello, ok I am trying to make a VB script to make a string with no spaces, have spaces added after every 2nd char, Basically make a hex converter, well sort of. Ok, this what I have:

1: Dim i As Integer 2: Dim old, tnew 3:
4: old = Text1.Text 5: tnew = Text2.Text 6:
7: For i = 0 To Len(old) 8: tnew = tnew & (Mid(old, i + 1, 1)) & " " 9: Next i 10: Text2.Text = tnew

This makes a space between every character: ex: hello! [converts to] h e l l o !

But I need it to be: hello! [converts to] he ll o!

I have tried: (Mid(old, i + 1, 2)) but this happens:

hello! [converts to] he el ll lo o! !

it seems to be copying the last character and then adding it to the next before continuing on.

Does anyone have any suggestions?

Thanks, -[El Miguel]-


ghost's Avatar
0 0

Try Mid(old, i, 2) and incrementing i by 2 each time through the loop?

Maybe something like:

7: For i = 0 To Len(old) Step 2 8: tnew = tnew & (Mid(old, i, 2)) & " " 9: Next i


elmiguel's Avatar
Member
2,795 1

Thanks, it worked, forgot about the "step", kinda kicking myself for not seeing it,

Thanks again,


ghost's Avatar
0 0

No worries