Results 1 to 10 of 10

Thread: Split function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Malaysia
    Posts
    90
    Private Sub command1_click()
    Dim strtext As String
    Dim iCounter As Integer
    Dim arrtxt() As String


    strtext = "Oops Not Working"
    arrtxt = Split(strtxt, " ")

    For iCounter = LBound(arrText) To UBound(arrText)
    MsgBox arrText(iCounter)
    Next
    Loop

    End Sub

    ** Above sample code work fine for me. However if i added few lines as shown below, the split result will not work! it will split the words according but to only display a single line.


    private sub command1_click()
    Dim strtext As String
    Dim iCounter As Integer
    Dim arrtxt() As String

    mfile = "c:\my documents\test.txt"

    x = FreeFile
    Open mfile For Input As #x

    Do Until EOF(x)
    Line Input #x, strtext

    arrText = Split(strtext, " ")

    For iCounter = LBound(arrText) To UBound(arrText)
    msgbox(arrtext(icounter)
    next
    loop

    end sub

    ** Above code is unable to find the " " inside every line i try to read. iCounter indicated a zero and the whole line was displayed rather than every word delimited by spaces.

    Regards
    CT

  2. #2
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    Originally posted by CT
    Private Sub command1_click()
    Dim strtext As String
    Dim iCounter As Integer
    Dim arrtxt() As String


    strtext = "Oops Not Working"
    arrtxt = Split(strtxt, " ")

    For iCounter = LBound(arrText) To UBound(arrText)
    MsgBox arrText(iCounter)
    Next
    Loop

    End Sub

    ** Above sample code work fine for me. However if i added few lines as shown below, the split result will not work! it will split the words according but to only display a single line.


    private sub command1_click()
    Dim strtext As String
    Dim iCounter As Integer
    Dim arrtxt() As String

    mfile = "c:\my documents\test.txt"

    x = FreeFile
    Open mfile For Input As #x

    Do Until EOF(x)
    Line Input #x, strtext

    arrText = Split(strtext, " ")

    For iCounter = LBound(arrText) To UBound(arrText)
    msgbox(arrtext(icounter)
    next iCounter ' Try that it might help
    loop

    end sub

    ** Above code is unable to find the " " inside every line i try to read. iCounter indicated a zero and the whole line was displayed rather than every word delimited by spaces.

    Regards
    Look in the code for my idea

    []P

    [Edited by PRIVATE1 on 10-18-2000 at 01:49 PM]
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Malaysia
    Posts
    90
    Sorry it didn't work. I am curious as to how icounter indicated a zero while it can displayed the whole line of text???
    CT

  4. #4
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    Is your file one long string with no Carrige returns or Line feeds ? I believe "Lineinput reads till it encounters one . I May be wrong. anyone else ?

    []P
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  5. #5
    Guest
    Is this what you want?

    Code:
    Private Sub Command1_Click()
    Dim strtext As String
    Dim iCounter As Integer
    Dim arrtxt() As String
    
    mfile = "c:\my documents\test.txt"
    
    x = FreeFile
    Open mfile For Input As #x
    
    Do Until EOF(x)
    Line Input #x, strtext
    
    arrtext = Split(strtext, " ")
    
    For iCounter = LBound(arrtext) To UBound(arrtext)
    i = i & (arrtext(iCounter))
    Next
    Loop
    MsgBox i
    
    End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Malaysia
    Posts
    90
    Thank you for all the helps provided but the code just refused to work.
    Inside my text file i have say 3 lines of text as follow:

    'some line sample from my test.txt
    Oops not working
    It's cool
    Who is winning


    the statements I used to split above lines are as follow:
    Line Input #x, strtext
    arrtext = split(strtext, " ")


    The result I expect to get from split function should be

    line 1:
    Oops
    not
    working

    line 2:
    It's
    cool

    I am not sure why the coding is not working. It seem like the split function is unable to locate the space (" ") between these lines. If i do a msgbox(arrtext(icounter) inside the for loop, I get the whole line i.e Oops not working

    I can get the result i wanted if I used the following statement:

    strtext = "Oops not working"
    arrtext = split(strtext," ")


    But this is not what I have in mind.

    CT

  7. #7
    Guest
    So you want to create a new line for each?

    Code:
    Private Sub Command1_Click()
    Dim strtext As String
    Dim iCounter As Integer
    Dim arrtxt() As String
    
    mfile = "c:\my documents\test.txt"
    
    x = FreeFile
    Open mfile For Input As #x
    
    Do Until EOF(x)
    Line Input #x, strtext
    
    arrtext = Split(strtext, " ")
    
    For iCounter = LBound(arrtext) To UBound(arrtext)
    i = i & (arrtext(iCounter)) & vbNewLine
    Next
    Loop
    MsgBox i
    
    End Sub

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Malaysia
    Posts
    90
    Matthew again thank you for your help. Actually the code you have mentioned earlier does not work in the first place i.e. i = i & (arrtext(icounter)) - it displayed the whole line of text.

    I am not looking for a new line. I need to split the words inside the line input correctly. The only problem i have here is the split function failed if i used arrtext = split(strtext," ").

    If I changed from space delimited to coma delimited inside the test.txt and used this statement arrtext = split(strtext,","). this damn program work fine for me.

    Regards
    CT

  9. #9
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'I added a list box for display
    
    Option Explicit
    
    Private Sub Command1_Click()
        Dim myVar As Variant  'must be variant
      
        Dim sfile As String, intNum As Integer
        intNum = FreeFile
        sfile = "C:\my Documents\myfile.txt"
        'contents of this file is as yours  Oops etc.
    
        Open sfile For Input As intNum
        
    Do While Not EOF(intNum)
           Line Input #intNum, myVar
     'split on the space   
           myVar = Split(myVar, " ")
    'read back the array created by the split      
     Dim i As Integer
          For i = LBound(myVar) To UBound(myVar)
            List1.AddItem myVar(i)
          Next i
          Loop
        Close intNum
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Malaysia
    Posts
    90
    Soory try your code but the list box just displayed a complete line like "Oops not working". Failed to locate the spaces inside these lines.

    The split function should displayed
    Oops
    Not
    working

    BTW, the txt file i used is created by notepad and save as text documents and I am using VB6 sp4.

    Again thank you very much for trying to help


    Regards

    CT

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