Click to See Complete Forum and Search --> : How do I Take a large string and divide it up into smaller ones?
Aaron
October 22nd, 1999, 09:10 AM
What is the best way to take a large string like a line of text read from a file and break it up into smaller chunks. For example if I had the following txt file parts (not all of the strings are the same size)
The text file would look like:
Hi My name Is Dork
What Kinda Name Is That Moron
Now I want to take this file and read in the lines and divide the individual lines up into individual strings like this:
a = Hi
b = My
c = Name
d = Is
e = Dork
a = What
b = kinda
c = Name
d = Is
e = That
f = Moron
Perhaps this could be in an array?!?
Thanks,
Aaron
Eis
October 22nd, 1999, 09:56 AM
the function you search is :
Mid(string, start[, length])
the rest :
read the string
search for space
split the string
and yes an array makes things easier
Aaron Young
October 22nd, 1999, 11:31 AM
If you have VB6 you can use the Split Function to Split the Text up Automatically into an Array of Individual Words, eg.
Dim sLines as Variant
sLines = Split(sText, " ")
If you don't have VB6, here's a Function to do the Same thing:
Function SplitText(byval sText as string, byval sSeperator as string) as Variant
Dim sLines() as string
Dim iLine as Long
While InStr(sText, sSeperator)
ReDim Preserve sLines(iLine)
sLines(iLine) = Left(sText, InStr(sText, sSeperator) - 1)
sText = mid(sText, len(sLines(iLine)) + (len(sSeperator) + 1))
iLine = iLine + 1
Wend
If len(sText) then
ReDim Preserve sLines(iLine)
sLines(iLine) = sText
End If
SplitText = sLines
End Function
You call it in the Same way as Split, eg.
sLines = SplitText(sText, " ")
Aaron Young
Analyst Programmer
adyoung@win.bright.net
aarony@redwingsoftware.com
deepak_warrier
October 23rd, 1999, 05:43 AM
After getting the words as Aaron Young says, store the strings in a "Collection" if the number of words is not known beforehand. You can also use the "ReDim" and "Preserve" statements if you are going to use an array. However, a "Collection" is preferable.
games-hints
October 23rd, 1999, 09:07 AM
OK.. so it split up the words. How do you get the words it split up. I can't get it to work.
Thanks
-Erik
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.