• 0

[VB6] Separating Words in a String


Question

I've tried, but my code isnt working!

Dim All As New Collection
j = 0
k = 1
Do Until k = 0
If j <> 0 Then k = InStr(j, txt, " ") Else k = InStr(txt, " ")
If k = 0 Then GoTo getout
n = InStr(k + 1, txt, " ") - k - 1
k = k + 1
All.Add Mid(txt, k, n)
j = k + n + 1
getout:
Loop

-anyone know my problem? <the code's a bit clumsy>

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Here ya go...

Dim All As New Collection

k = 1

Do Until k = 0

n = InStr(k + 1, txt, " ")

If n = 0 Then n = Len(txt) + 1

j = n - k

All.Add Mid(txt, k, j)

k = InStr(k + j, txt, " ") + 1

If k = 1 Then k = 0

Loop

Link to comment
Share on other sites

  • 0

actually, there is one small problem:

it only picks up one space which leaves words after a period (with double spacing, this is) with a space at the start of the string. I've attempted, but it doesnt work :cry:

my code:

k = 1

Do Until k = 0

n = InStr(k + 1, txt, " ")

If n = 0 Then n = Len(txt) + 1

j = n - k

Do Until Mid(txt, k, 1) <> " "

txt = Mid(txt, 1, k - 1) & Mid(txt, k + 1)

Loop

All.Add Mid(txt, k, j)

k = InStr(k + j, txt, " ") + 1

If k = 1 Then k = 0

Loop

-any help?

Link to comment
Share on other sites

  • 0

Dim All As New Collection

k = 1

Do Until k = 0

n = InStr(k + 1, txt, " ")

If n = 0 Then n = Len(txt) + 1

j = n - k

All.Add Mid(txt, k, j)

k = InStr(k + j, txt, " ") + 1

If k = 1 Then

k = 0

Else

Do Until Mid(txt, k, 1) <> " "

k = k + 1

Loop

End If

Loop

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.