CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Location
    Patna,India
    Posts
    38

    Split string using tab,space as delimeter

    I read a txt file in a variable and split it in array. Field seperator used in my file is Tab or space (space can be one or more). when i split it by using delimeter TAB then it skips space. pls tell me how can it read space (any number of space)and Tab.

    example of txt file

    1 0 100 100
    2 101 175 75
    3 176 250 75
    4 251 500 250

  2. #2
    Join Date
    Apr 2004
    Posts
    14

    Use regex

    in vb 6, SPLIT function does not allow regular expression as a delimiter, but may be you can try this trick

    - use regular expression to replace the space(es) or tab(s) to a single character. for example ":".
    - next you can use split function to split the result string with delimiter ":".

    NOTE : i see that your text example doesn't contain ":" char, so you can do the splitting easily, but if your text file can contain any character (not only number, "+","-",",") you should implement more advance technique.

    -Happy coding-

  3. #3
    Join Date
    Aug 2003
    Location
    Gardnerville,NV
    Posts
    199
    Here is a hack that splits the string base on a space, then splits the splits based on a Tab and puts the whole thing in an array
    kevin
    Code:
     
    Private Sub Form_Load()
    Dim c()
    
        s = "1 2" & vbTab & _
            "3 4" & vbTab & _
            "5 6" & vbTab & _
            "7 8" & vbTab & _
            "9 0" & vbTab & "end"
            
        a = Split(s, " ")
        For i = 0 To UBound(a)
            b = Split(a(i), vbTab)
            For j = 0 To UBound(b)
                ReDim Preserve c(k)
                c(k) = b(j)
                k = k + 1
            Next j
        Next i
       ' the split values should now be in the c() array
    End Sub
    Thank you for your support.
    -Bartyles & James (circa 1980)


    If it helps, rate it
    if it doesn't, well....I tried


    Check out Windows Embedded XP. It's XP compontnetized: add only the components you need to build an OS

    And in knowing that you know nothing, that makes you the smartest of all.
    - Socrates (469 BC - 399 BC)

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    Originally posted by kebo
    Here is a hack that splits the string base on a space, then splits the splits based on a Tab and puts the whole thing in an array
    kevin
    Code:
     
    Private Sub Form_Load()
    Dim c()
    
        s = "1 2" & vbTab & _
            "3 4" & vbTab & _
            "5 6" & vbTab & _
            "7 8" & vbTab & _
            "9 0" & vbTab & "end"
            
        a = Split(s, " ")
        For i = 0 To UBound(a)
            b = Split(a(i), vbTab)
            For j = 0 To UBound(b)
                ReDim Preserve c(k)
                c(k) = b(j)
                k = k + 1
            Next j
        Next i
       ' the split values should now be in the c() array
    End Sub
    all that redim preserving might make the program wild for memory use or very slow...

    I vote the first suggestion to be very good.. replace every occurrence of space with something that is not in the file..

    like maybe Chr$(0)

    then replace every occurrence of tab, with that same Chr$(0)

    Then split the string based on Chr$(0)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Apr 2004
    Location
    Patna,India
    Posts
    38
    I used regular expression for replace space , tab and it is working fine. Code posted by kebo is also working fine. I decided to use first method.
    Thanks a lot all of u for given your precious time.

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