CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 1999
    Posts
    21

    How do I Take a large string and divide it up into smaller ones?

    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


  2. #2
    Join Date
    Oct 1999
    Location
    germany
    Posts
    8

    Re: How do I Take a large string and divide it up into smaller ones?

    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




  3. #3
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: How do I Take a large string and divide it up into smaller ones?

    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
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  4. #4
    Join Date
    Sep 1999
    Location
    Trivandrum, Kerala, INDIA.
    Posts
    32

    Re: How do I Take a large string and divide it up into smaller ones?

    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.


  5. #5
    Join Date
    Oct 1999
    Posts
    5

    Re: How do I Take a large string and divide it up into smaller ones?

    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured