Click to See Complete Forum and Search --> : Separating string
QWERTY
Dec 3rd, 1999, 11:23 AM
What would be the most efficient way to separate this kind of string: "Word1 Word2 Word3 Word4 Word5", and put each word to separate textbox?
Thanks in advance.
------------------
Visual Basic Programmer (at least I want to be one)
------------------
PolComSoft
You will hear a lot about it.
MartinLiss
Dec 3rd, 1999, 12:58 PM
If you have VB6 you can use the Split function like this: Dim strResults() As String
Dim intCount As Integer
Const strText = "Word1 Word2 Word3 Word4 Word5"
strResults() = Split(strText, " ")
For intCount = 0 To UBound(strResults)
myTextbox(intCount).Text = strResults(intCount)
Next intCount
The above assumes your TextBoxes are a control array. If not then you'll need to remove the loop and move the strResults entries individually.
------------------
Marty
QWERTY
Dec 3rd, 1999, 01:25 PM
Unfortunately I don't have VB6
------------------
Visual Basic Programmer (at least I want to be one)
------------------
PolComSoft
You will hear a lot about it.
Gerald
Dec 3rd, 1999, 10:30 PM
Here is a word splitting function that will return the words in an array.
Public Function WordSplit(sWords As String) As Variant
Dim sTemp As String
Dim vWords() As Variant
Dim n, nCount As Integer
nCount = 0
sTemp = sWords
Do
n = InStr(1, sTemp, " ")
ReDim Preserve vWords(nCount)
If n = 0 Then
vWords(nCount) = sTemp
Else
vWords(nCount) = Left$(sTemp, n)
sTemp = Mid$(sTemp, n + 1)
End If
nCount = nCount + 1
Loop Until n = 0
WordSplit = vWords
End Function
-- Gerald M
MartinLiss
Dec 3rd, 1999, 10:40 PM
OK, do the following instead: Dim intStartPos As Integer
Dim intSpacePos As Integer
Dim intOldPos As Integer
Const strText = "Word1 Word2 Word3 Word4 Word5"
intStartPos = 1
intSpacePos = -1
'Parse the semicolon-delimited input record, looping until no spaces
'are found
Do Until intSpacePos = 0
intSpacePos = InStr(intStartPos, strText, " ", 1)
If intSpacePos > 0 Then
MsgBox Mid(strText, intStartPos, intSpacePos - intOldPos)
Else
Exit Do
End If
intStartPos = intSpacePos + 1
intOldPos = intStartPos
Loop
Replace the MsgBox statement with something that updates your textboxes.
------------------
Marty
Gerald
Dec 4th, 1999, 06:42 AM
Thank you Marty. I'm aware of the overhead involved with using variant data types, but I completely forgot about the Dim statement in regards to explicitly defining the data type.
Thanks again for reminding me, Gerald. :)
Private Sub Command1_Click()
Dim sText As String
Dim aWords() As String
Dim n As Integer, nMax As Integer
sText = "Word1 Word2 Word3 Word4 Word5"
WordSplit sText, aWords
nMax = UBound(aWords)
For n = 0 To nMax
' Update your text boxes.
Next
End Sub
Public Sub WordSplit(s As String, aWords() As String)
Dim sTemp As String
Dim n As Integer, nCount As Integer
nCount = 0
sTemp = s
Do
n = InStr(1, sTemp, " ")
ReDim Preserve aWords(nCount)
If n = 0 Then
aWords(nCount) = sTemp
Else
aWords(nCount) = Left$(sTemp, n)
sTemp = Mid$(sTemp, n + 1)
End If
nCount = nCount + 1
Loop Until n = 0
End Sub
MartinLiss
Dec 4th, 1999, 11:11 AM
Gerald: Did you know that when you dimension variables as you did with Dim n, nCount As Integer, that n is a then a Variant rather than an Integer? Also, using variants is slow, so you should probably change vWords to sWords and dimension it as a String.
------------------
Marty
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.