merdik
asked on
Problem with string manipulation
Hi,
I have for example "Bill Clinton 50000 washington" in a string (I have lots of string like that)
What I want is to put "Bill" in a variable
"Clinton" to another variable
"50000" to another variable
"washington" to another variable
How do I do that?!?!?!
I have for example "Bill Clinton 50000 washington" in a string (I have lots of string like that)
What I want is to put "Bill" in a variable
"Clinton" to another variable
"50000" to another variable
"washington" to another variable
How do I do that?!?!?!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DIM txt(10) as array
RefString = "Bill Clinton 50000 Washington"
pointer = 0
'
Retry:
marker = instr( RefString, " " )
if marker > 0 then
txt( pointer ) = left( RefString, marker -1 )
pointer = pointer + 1
RefString = trim( right( RefString, len( RefString ) - marker ) )
goto retry
endif
M
RefString = "Bill Clinton 50000 Washington"
pointer = 0
'
Retry:
marker = instr( RefString, " " )
if marker > 0 then
txt( pointer ) = left( RefString, marker -1 )
pointer = pointer + 1
RefString = trim( right( RefString, len( RefString ) - marker ) )
goto retry
endif
M
Try the strtok function, it will return each part at a time. first call it with the string, and then inside a loop with ""
'PURPOSE: Splits a string to tokens
'ARGUMENTS:
' IN szString: the string to tokenise. Should be the string itself on the first call,
' and "" on the following calls.
' IN szSeparators: The characters that act as separators between the tokens in the
' string, can be more then 1 character
'RETURNS: The next token in the string, or "" if nothing is left
'
'NOTES: This uses static variable, hence only 1 string can be tokenised at one time.
' Passing a string as the first parameter overrides previos calls, and the string
' processing starts over. For more info, see help on 'strtok' in the C library reference
'
Public Function strtok(szString As String, szSeparators As String) As String
Static szBuf As String 'Static buffer to store the string
Dim I As Integer
Dim iPos2 As Integer
Static iPos As Integer
Dim iTmp As Integer
Dim iPos1 As Integer
Dim bSet As Boolean
If szString <> "" Then szBuf = szString 'Update to a new string
If iPos = 0 Then iPos = 1
iPos2 = iPos
iTmp = Len(szSeparators)
'Iterate through all the separators, and find the position of the closes next occurence
'of any of them
For I = 1 To iTmp
iPos1 = InStr(iPos2 + 1, szBuf, Mid(szSeparators, I, 1))
If iPos1 > 0 Then
If Not bSet Then
iPos = iPos1
Else
If iPos1 < iPos Then iPos = iPos1
End If
bSet = True
End If
Next I
'If no more separators, means the string has ended, return all of it
If Not bSet Then
strtok = Right$(szBuf, Len(szBuf) - iPos)
iPos = Len(szBuf)
Exit Function
End If
If iPos2 = 1 Then
strtok = Mid(szBuf, iPos2, iPos - iPos2)
Else
strtok = Mid(szBuf, iPos2 + 1, iPos - iPos2 - 1)
End If
End Function
'PURPOSE: Splits a string to tokens
'ARGUMENTS:
' IN szString: the string to tokenise. Should be the string itself on the first call,
' and "" on the following calls.
' IN szSeparators: The characters that act as separators between the tokens in the
' string, can be more then 1 character
'RETURNS: The next token in the string, or "" if nothing is left
'
'NOTES: This uses static variable, hence only 1 string can be tokenised at one time.
' Passing a string as the first parameter overrides previos calls, and the string
' processing starts over. For more info, see help on 'strtok' in the C library reference
'
Public Function strtok(szString As String, szSeparators As String) As String
Static szBuf As String 'Static buffer to store the string
Dim I As Integer
Dim iPos2 As Integer
Static iPos As Integer
Dim iTmp As Integer
Dim iPos1 As Integer
Dim bSet As Boolean
If szString <> "" Then szBuf = szString 'Update to a new string
If iPos = 0 Then iPos = 1
iPos2 = iPos
iTmp = Len(szSeparators)
'Iterate through all the separators, and find the position of the closes next occurence
'of any of them
For I = 1 To iTmp
iPos1 = InStr(iPos2 + 1, szBuf, Mid(szSeparators, I, 1))
If iPos1 > 0 Then
If Not bSet Then
iPos = iPos1
Else
If iPos1 < iPos Then iPos = iPos1
End If
bSet = True
End If
Next I
'If no more separators, means the string has ended, return all of it
If Not bSet Then
strtok = Right$(szBuf, Len(szBuf) - iPos)
iPos = Len(szBuf)
Exit Function
End If
If iPos2 = 1 Then
strtok = Mid(szBuf, iPos2, iPos - iPos2)
Else
strtok = Mid(szBuf, iPos2 + 1, iPos - iPos2 - 1)
End If
End Function
ASKER
OK but:
for a = 1 to 5
Text1.Text = strtok("hello Bill", "")
next a
does'nt work I know that I have to tell him to print each word in diferent textbox but how?
for a = 1 to 5
Text1.Text = strtok("hello Bill", "")
next a
does'nt work I know that I have to tell him to print each word in diferent textbox but how?
Change my example from DIM txt(10) to use a control array for the text items. Then you can code:
Text1( pointer ).Text = Left(...)
M
Text1( pointer ).Text = Left(...)
M
DIm S as string
S=strtok("Word1 Word2 Word3 Word4", " ")
While S<>""
S=strtok("", " ")
'assign to textbox here
Wend
S=strtok("Word1 Word2 Word3 Word4", " ")
While S<>""
S=strtok("", " ")
'assign to textbox here
Wend
?
Merdik,
I appreciate that you are a beginner, but I would encourage you not to give up so quickly.
You say you don't know how to use Instr. OK, I will include some sample code that shows you how the concepts explained in my answer can be implemented.
To understand how a VB function or keyword works it is definitely worth your while reading the help on it. The easiest way to do this is to highlight the word in question and hit the F1 key for help. I found the help in VB most useful.
With firstpos I mean the first position at which I found a blank. By the way, in the implementation added to this comment I have not needed to use this as I've used Mid for all words except the last one.
To see how the example works, create a standard VB form, put a text box on it (accept the default name of Text1), and put a command button on it (accept the default name of Command1). Paste the code into the form's code module.
When you type a sentence into the text box and hit the command button a message box will pop up with each individual word in it.
---code follows:
Private Sub Command1_Click()
Dim strToSearch As String
Dim intPos1 As Integer
Dim intPos2 As Integer
strToSearch = Text1.Text
intPos1 = 0
'loop to find all spaces in the string.
intPos2 = InStr(strToSearch, " ")
Do While intPos2 <> 0
'show the current "word" to the user
MsgBox Mid$(strToSearch, intPos1 + 1, intPos2 - intPos1 - 1)
intPos1 = intPos2
intPos2 = InStr(intPos1 + 1, strToSearch, " ")
Loop
'show last word
MsgBox Right(strToSearch, Len(strToSearch) - intPos1)
End Sub
I appreciate that you are a beginner, but I would encourage you not to give up so quickly.
You say you don't know how to use Instr. OK, I will include some sample code that shows you how the concepts explained in my answer can be implemented.
To understand how a VB function or keyword works it is definitely worth your while reading the help on it. The easiest way to do this is to highlight the word in question and hit the F1 key for help. I found the help in VB most useful.
With firstpos I mean the first position at which I found a blank. By the way, in the implementation added to this comment I have not needed to use this as I've used Mid for all words except the last one.
To see how the example works, create a standard VB form, put a text box on it (accept the default name of Text1), and put a command button on it (accept the default name of Command1). Paste the code into the form's code module.
When you type a sentence into the text box and hit the command button a message box will pop up with each individual word in it.
---code follows:
Private Sub Command1_Click()
Dim strToSearch As String
Dim intPos1 As Integer
Dim intPos2 As Integer
strToSearch = Text1.Text
intPos1 = 0
'loop to find all spaces in the string.
intPos2 = InStr(strToSearch, " ")
Do While intPos2 <> 0
'show the current "word" to the user
MsgBox Mid$(strToSearch, intPos1 + 1, intPos2 - intPos1 - 1)
intPos1 = intPos2
intPos2 = InStr(intPos1 + 1, strToSearch, " ")
Loop
'show last word
MsgBox Right(strToSearch, Len(strToSearch) - intPos1)
End Sub
ASKER
OK tanks a lot... it work now... (By the way, I don't have the vb help file)
So, you'll accept the answer then?
Glad to be of help.
Good luck!
Glad to be of help.
Good luck!
ASKER
oups... i've forgot to give you my points...
ASKER
I'm a beginer so...
How do I know the "firstpos" variable?
and How do I use instr ??