converting tabs into spaces?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • p s

    #1

    converting tabs into spaces?

    hi all
    i have a vb6 project, one of the functions is to read in a text file and
    place it into an array
    problem is, when people use the TAB key in the original file that is read, i
    just get the tab code in the string,
    what i need is for the TAB to literally become that many SPACES ... the
    problem is this
    can vary depending on where exactly the TAB key is hit!
    i have tried

    sOrigString = Replace(sOrigSt ring, vbTab, " ")

    but this just puts in one space
    of course if someone hits the tab key at the start of the line it will jump
    the cursor further than if someone hit it a few characters in (if you know
    what i mean!)

    can anybody help?

    thanks
    pete


  • steve

    #2
    Re: converting tabs into spaces?

    On Tue, 22 Jul 2003 15:15:07 GMT, "p s" <pete_smith_uk@ hotmail.com>
    wrote:
    [color=blue]
    >hi all
    >i have a vb6 project, one of the functions is to read in a text file and
    >place it into an array
    >problem is, when people use the TAB key in the original file that is read, i
    >just get the tab code in the string,
    >what i need is for the TAB to literally become that many SPACES ... the
    >problem is this
    >can vary depending on where exactly the TAB key is hit!
    >i have tried
    >
    >sOrigString = Replace(sOrigSt ring, vbTab, " ")
    >
    >but this just puts in one space
    >of course if someone hits the tab key at the start of the line it will jump
    >the cursor further than if someone hit it a few characters in (if you know
    >what i mean!)
    >
    >can anybody help?
    >
    >thanks
    >pete
    >[/color]

    I'm not sure if you can get the tab size programatically without some
    fairly complex complex coding. If you know the tab size you could just
    use something like
    sOrigString = Replace(sOrigSt ring, vbTab, string(nTabSize , " "))

    where tabsize is set to 4 or 8 or whatever you tabsize is.


    steve

    Comment

    • p s

      #3
      Re: converting tabs into spaces?


      "steve hotmail.com>" <stesrc@<remove .this.antispam. bit> wrote in message
      news:vhlqhvc70t reihtps8b6b9d9j 3k9jj90v8@4ax.c om...[color=blue]
      > On Tue, 22 Jul 2003 15:15:07 GMT, "p s" <pete_smith_uk@ hotmail.com>
      > wrote:
      >[color=green]
      > >hi all
      > >i have a vb6 project, one of the functions is to read in a text file and
      > >place it into an array
      > >problem is, when people use the TAB key in the original file that is[/color][/color]
      read, i[color=blue][color=green]
      > >just get the tab code in the string,
      > >what i need is for the TAB to literally become that many SPACES ... the
      > >problem is this
      > >can vary depending on where exactly the TAB key is hit!
      > >i have tried
      > >
      > >sOrigString = Replace(sOrigSt ring, vbTab, " ")
      > >
      > >but this just puts in one space
      > >of course if someone hits the tab key at the start of the line it will[/color][/color]
      jump[color=blue][color=green]
      > >the cursor further than if someone hit it a few characters in (if you[/color][/color]
      know[color=blue][color=green]
      > >what i mean!)
      > >
      > >can anybody help?
      > >
      > >thanks
      > >pete
      > >[/color]
      >
      > I'm not sure if you can get the tab size programatically without some
      > fairly complex complex coding. If you know the tab size you could just
      > use something like
      > sOrigString = Replace(sOrigSt ring, vbTab, string(nTabSize , " "))
      >
      > where tabsize is set to 4 or 8 or whatever you tabsize is.
      >
      >
      > steve[/color]


      thanks steve...
      but doesnt the actual amount of spaces change depending on where you are in
      the line?
      e.g. tab stops are set up every 8th character... tabbing at start of the
      line will mean nTabSize =8 , 5 chars in and nTabSize=3
      ?
      cheers
      pete


      Comment

      • steve

        #4
        Re: converting tabs into spaces?

        On Tue, 22 Jul 2003 15:43:44 GMT, "p s" <pete_smith_uk@ hotmail.com>
        wrote:
        [color=blue]
        >
        >"steve hotmail.com>" <stesrc@<remove .this.antispam. bit> wrote in message
        >news:vhlqhvc70 treihtps8b6b9d9 j3k9jj90v8@4ax. com...[color=green]
        >> On Tue, 22 Jul 2003 15:15:07 GMT, "p s" <pete_smith_uk@ hotmail.com>
        >> wrote:
        >>[color=darkred]
        >> >hi all
        >> >i have a vb6 project, one of the functions is to read in a text file and
        >> >place it into an array
        >> >problem is, when people use the TAB key in the original file that is[/color][/color]
        >read, i[color=green][color=darkred]
        >> >just get the tab code in the string,
        >> >what i need is for the TAB to literally become that many SPACES ... the
        >> >problem is this
        >> >can vary depending on where exactly the TAB key is hit!
        >> >i have tried
        >> >
        >> >sOrigString = Replace(sOrigSt ring, vbTab, " ")
        >> >
        >> >but this just puts in one space
        >> >of course if someone hits the tab key at the start of the line it will[/color][/color]
        >jump[color=green][color=darkred]
        >> >the cursor further than if someone hit it a few characters in (if you[/color][/color]
        >know[color=green][color=darkred]
        >> >what i mean!)
        >> >
        >> >can anybody help?
        >> >
        >> >thanks
        >> >pete
        >> >[/color]
        >>
        >> I'm not sure if you can get the tab size programatically without some
        >> fairly complex complex coding. If you know the tab size you could just
        >> use something like
        >> sOrigString = Replace(sOrigSt ring, vbTab, string(nTabSize , " "))
        >>
        >> where tabsize is set to 4 or 8 or whatever you tabsize is.
        >>
        >>
        >> steve[/color]
        >
        >
        >thanks steve...
        >but doesnt the actual amount of spaces change depending on where you are in
        >the line?
        >e.g. tab stops are set up every 8th character... tabbing at start of the
        >line will mean nTabSize =8 , 5 chars in and nTabSize=3
        >?
        >cheers
        >pete
        >[/color]


        Yes it does change which is where the complex coding comes in.

        Maybe you could read the line up to a tab chr. Then pad out with
        spaces up to the next tab position (given a fixed tab size) then
        repeat the process until you are finished.

        I've done a similar thing building strings but not on strings being
        read then converted. I'll dig out the code if you think it might help


        steve

        Comment

        • p s

          #5
          Re: converting tabs into spaces?

          > Yes it does change which is where the complex coding comes in.[color=blue]
          >
          > Maybe you could read the line up to a tab chr. Then pad out with
          > spaces up to the next tab position (given a fixed tab size) then
          > repeat the process until you are finished.
          >
          > I've done a similar thing building strings but not on strings being
          > read then converted. I'll dig out the code if you think it might help
          >
          >
          > steve[/color]

          thanks steve,

          i thought i may have to take this aproach... but then wondered if anyone had
          already done it as it seems like it should be a common problem
          is there a fixed tab size i should look at, out of interest? these are files
          created in windows notepad. can i assume a default?

          cheers
          pete


          Comment

          • Tony Proctor

            #6
            Re: converting tabs into spaces?

            In general, a TAB doesn't correspond to a fixed number of spaces, or even a
            fixed destination column. In ANSI terms, it's a "format effector" rather
            than a "graphic character".

            However, if you're using the old VDU convention of 8-character TAB settings,
            then this might help:

            Private Function iNext(iCh As Integer) As Integer
            ' Returns the next TAB-column position from the current character position
            iNext = 8 * ((iCh \ 8) + 1)
            End Function

            If you have a TAB at character position iCh then you would simply have to
            replace it by

            Space$(iNext(iC h)-iCh)


            Tony

            "p s" <pete_smith_uk@ hotmail.com> wrote in message
            news:%xcTa.147$ Ci7.2047136@new s-text.cableinet. net...[color=blue]
            > hi all
            > i have a vb6 project, one of the functions is to read in a text file and
            > place it into an array
            > problem is, when people use the TAB key in the original file that is read,[/color]
            i[color=blue]
            > just get the tab code in the string,
            > what i need is for the TAB to literally become that many SPACES ... the
            > problem is this
            > can vary depending on where exactly the TAB key is hit!
            > i have tried
            >
            > sOrigString = Replace(sOrigSt ring, vbTab, " ")
            >
            > but this just puts in one space
            > of course if someone hits the tab key at the start of the line it will[/color]
            jump[color=blue]
            > the cursor further than if someone hit it a few characters in (if you know
            > what i mean!)
            >
            > can anybody help?
            >
            > thanks
            > pete
            >
            >[/color]


            Comment

            • Al

              #7
              Re: converting tabs into spaces?

              why don't you setup a temp table or array with fields
              explain.
              assuming that your .TXT file is as follow

              John Doe Somewhere in Usa New York, NY 10005 555-5555
              president
              John Doe Somewhere in Usa New York, NY 10005 555-5555
              president
              John Doe Somewhere in Usa New York, NY 10005 555-5555
              president
              John Doe Somewhere in Usa New York, NY 10005 555-5555
              president
              John Doe Somewhere in Usa New York, NY 10005 555-5555
              president


              '- your text file setting.
              There is 16 space starting from "John Doe" to (Somewhere in usa)
              There is 30 space starting from "Somewhere in usa" to (New Yok, NY 10005)
              There is 20 space starting from "New York, NY 10005" to (555-5555)
              There is 8 space starting "555-55555" to (president)
              There is 25 space starting from "president" to (blank spaces)

              '- Array or TempTable. (Fields)
              field1 = Name Text (16)
              field2 = Address Text (30)
              field3 = CityStaZip Text (20)
              field4 = Telephone Text (8)
              field5 = Title Text (25)

              '- Read the File
              OPEN "TEXT FILE" for input as #1
              input #1 field1, field2, field3, field4, field5


              I hope this is what you are looking for.
              Good Luck...!

              "p s" <pete_smith_uk@ hotmail.com> wrote in message
              news:%xcTa.147$ Ci7.2047136@new s-text.cableinet. net...[color=blue]
              > hi all
              > i have a vb6 project, one of the functions is to read in a text file and
              > place it into an array
              > problem is, when people use the TAB key in the original file that is read,[/color]
              i[color=blue]
              > just get the tab code in the string,
              > what i need is for the TAB to literally become that many SPACES ... the
              > problem is this
              > can vary depending on where exactly the TAB key is hit!
              > i have tried
              >
              > sOrigString = Replace(sOrigSt ring, vbTab, " ")
              >
              > but this just puts in one space
              > of course if someone hits the tab key at the start of the line it will[/color]
              jump[color=blue]
              > the cursor further than if someone hit it a few characters in (if you know
              > what i mean!)
              >
              > can anybody help?
              >
              > thanks
              > pete
              >
              >[/color]


              Comment

              • Tony Proctor

                #8
                Re: converting tabs into spaces?

                Sorry, mixing-up my 1:n and 0:n-1 indexing. Should have been

                Private Function iNext(iCh As Integer) As Integer
                iNext = 8 * (((iCh - 1) \ 8) + 1) + 1
                End Function

                Tony

                "Tony Proctor" <tony_proctor@a imtechnology_NO SPAM.com> wrote in message
                news:e0jE2uGUDH A.1912@tk2msftn gp13.phx.gbl...[color=blue]
                > In general, a TAB doesn't correspond to a fixed number of spaces, or even[/color]
                a[color=blue]
                > fixed destination column. In ANSI terms, it's a "format effector" rather
                > than a "graphic character".
                >
                > However, if you're using the old VDU convention of 8-character TAB[/color]
                settings,[color=blue]
                > then this might help:
                >
                > Private Function iNext(iCh As Integer) As Integer
                > ' Returns the next TAB-column position from the current character position
                > iNext = 8 * ((iCh \ 8) + 1)
                > End Function
                >
                > If you have a TAB at character position iCh then you would simply have to
                > replace it by
                >
                > Space$(iNext(iC h)-iCh)
                >
                >
                > Tony
                >
                > "p s" <pete_smith_uk@ hotmail.com> wrote in message
                > news:%xcTa.147$ Ci7.2047136@new s-text.cableinet. net...[color=green]
                > > hi all
                > > i have a vb6 project, one of the functions is to read in a text file and
                > > place it into an array
                > > problem is, when people use the TAB key in the original file that is[/color][/color]
                read,[color=blue]
                > i[color=green]
                > > just get the tab code in the string,
                > > what i need is for the TAB to literally become that many SPACES ... the
                > > problem is this
                > > can vary depending on where exactly the TAB key is hit!
                > > i have tried
                > >
                > > sOrigString = Replace(sOrigSt ring, vbTab, " ")
                > >
                > > but this just puts in one space
                > > of course if someone hits the tab key at the start of the line it will[/color]
                > jump[color=green]
                > > the cursor further than if someone hit it a few characters in (if you[/color][/color]
                know[color=blue][color=green]
                > > what i mean!)
                > >
                > > can anybody help?
                > >
                > > thanks
                > > pete
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • p s

                  #9
                  Re: converting tabs into spaces?

                  thanks tony, same principle as norms code, exactly what i needed

                  "Tony Proctor" <tony_proctor@a imtechnology_NO SPAM.com> wrote in message
                  news:OTvEiyGUDH A.2520@tk2msftn gp13.phx.gbl...[color=blue]
                  > Sorry, mixing-up my 1:n and 0:n-1 indexing. Should have been
                  >
                  > Private Function iNext(iCh As Integer) As Integer
                  > iNext = 8 * (((iCh - 1) \ 8) + 1) + 1
                  > End Function
                  >
                  > Tony
                  >
                  > "Tony Proctor" <tony_proctor@a imtechnology_NO SPAM.com> wrote in message
                  > news:e0jE2uGUDH A.1912@tk2msftn gp13.phx.gbl...[color=green]
                  > > In general, a TAB doesn't correspond to a fixed number of spaces, or[/color][/color]
                  even[color=blue]
                  > a[color=green]
                  > > fixed destination column. In ANSI terms, it's a "format effector" rather
                  > > than a "graphic character".
                  > >
                  > > However, if you're using the old VDU convention of 8-character TAB[/color]
                  > settings,[color=green]
                  > > then this might help:
                  > >
                  > > Private Function iNext(iCh As Integer) As Integer
                  > > ' Returns the next TAB-column position from the current character[/color][/color]
                  position[color=blue][color=green]
                  > > iNext = 8 * ((iCh \ 8) + 1)
                  > > End Function
                  > >
                  > > If you have a TAB at character position iCh then you would simply have[/color][/color]
                  to[color=blue][color=green]
                  > > replace it by
                  > >
                  > > Space$(iNext(iC h)-iCh)
                  > >
                  > >
                  > > Tony
                  > >
                  > > "p s" <pete_smith_uk@ hotmail.com> wrote in message
                  > > news:%xcTa.147$ Ci7.2047136@new s-text.cableinet. net...[color=darkred]
                  > > > hi all
                  > > > i have a vb6 project, one of the functions is to read in a text file[/color][/color][/color]
                  and[color=blue][color=green][color=darkred]
                  > > > place it into an array
                  > > > problem is, when people use the TAB key in the original file that is[/color][/color]
                  > read,[color=green]
                  > > i[color=darkred]
                  > > > just get the tab code in the string,
                  > > > what i need is for the TAB to literally become that many SPACES ...[/color][/color][/color]
                  the[color=blue][color=green][color=darkred]
                  > > > problem is this
                  > > > can vary depending on where exactly the TAB key is hit!
                  > > > i have tried
                  > > >
                  > > > sOrigString = Replace(sOrigSt ring, vbTab, " ")
                  > > >
                  > > > but this just puts in one space
                  > > > of course if someone hits the tab key at the start of the line it will[/color]
                  > > jump[color=darkred]
                  > > > the cursor further than if someone hit it a few characters in (if you[/color][/color]
                  > know[color=green][color=darkred]
                  > > > what i mean!)
                  > > >
                  > > > can anybody help?
                  > > >
                  > > > thanks
                  > > > pete
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • p s

                    #10
                    Re: converting tabs into spaces?

                    cheers al
                    problem is my lines are varying ... they are not uniform as in your example
                    thanks again
                    pete

                    "Al" <alberto@mashis pano.com> wrote in message
                    news:uncmCvGUDH A.556@TK2MSFTNG P12.phx.gbl...[color=blue]
                    > why don't you setup a temp table or array with fields
                    > explain.
                    > assuming that your .TXT file is as follow
                    >
                    > John Doe Somewhere in Usa New York, NY 10005 555-5555
                    > president
                    > John Doe Somewhere in Usa New York, NY 10005 555-5555
                    > president
                    > John Doe Somewhere in Usa New York, NY 10005 555-5555
                    > president
                    > John Doe Somewhere in Usa New York, NY 10005 555-5555
                    > president
                    > John Doe Somewhere in Usa New York, NY 10005 555-5555
                    > president
                    >
                    >
                    > '- your text file setting.
                    > There is 16 space starting from "John Doe" to (Somewhere in usa)
                    > There is 30 space starting from "Somewhere in usa" to (New Yok, NY 10005)
                    > There is 20 space starting from "New York, NY 10005" to (555-5555)
                    > There is 8 space starting "555-55555" to (president)
                    > There is 25 space starting from "president" to (blank spaces)
                    >
                    > '- Array or TempTable. (Fields)
                    > field1 = Name Text (16)
                    > field2 = Address Text (30)
                    > field3 = CityStaZip Text (20)
                    > field4 = Telephone Text (8)
                    > field5 = Title Text (25)
                    >
                    > '- Read the File
                    > OPEN "TEXT FILE" for input as #1
                    > input #1 field1, field2, field3, field4, field5
                    >
                    >
                    > I hope this is what you are looking for.
                    > Good Luck...!
                    >
                    > "p s" <pete_smith_uk@ hotmail.com> wrote in message
                    > news:%xcTa.147$ Ci7.2047136@new s-text.cableinet. net...[color=green]
                    > > hi all
                    > > i have a vb6 project, one of the functions is to read in a text file and
                    > > place it into an array
                    > > problem is, when people use the TAB key in the original file that is[/color][/color]
                    read,[color=blue]
                    > i[color=green]
                    > > just get the tab code in the string,
                    > > what i need is for the TAB to literally become that many SPACES ... the
                    > > problem is this
                    > > can vary depending on where exactly the TAB key is hit!
                    > > i have tried
                    > >
                    > > sOrigString = Replace(sOrigSt ring, vbTab, " ")
                    > >
                    > > but this just puts in one space
                    > > of course if someone hits the tab key at the start of the line it will[/color]
                    > jump[color=green]
                    > > the cursor further than if someone hit it a few characters in (if you[/color][/color]
                    know[color=blue][color=green]
                    > > what i mean!)
                    > >
                    > > can anybody help?
                    > >
                    > > thanks
                    > > pete
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Rick Rothstein

                      #11
                      Re: converting tabs into spaces?

                      > i have a vb6 project, one of the functions is to read in a text file and[color=blue]
                      > place it into an array
                      > problem is, when people use the TAB key in the original file that is read,[/color]
                      i[color=blue]
                      > just get the tab code in the string,
                      > what i need is for the TAB to literally become that many SPACES ... the
                      > problem is this
                      > can vary depending on where exactly the TAB key is hit!
                      > i have tried
                      >
                      > sOrigString = Replace(sOrigSt ring, vbTab, " ")
                      >
                      > but this just puts in one space
                      > of course if someone hits the tab key at the start of the line it will[/color]
                      jump[color=blue]
                      > the cursor further than if someone hit it a few characters in (if you know
                      > what i mean!)
                      >
                      > can anybody help?[/color]

                      I've read through the thread and, if I understand where Tab places the next
                      print position correctly (an 8 space tab writes at positions 1,9,18,etc.;
                      otherwise adjust the TabOffset calculation if I'm wrong), wish to offer this
                      alternative to the other posts. I didn't check the other's codes, but in a
                      long string, I'm thinking this may be faster in that it uses a Mid$
                      statement as opposed to continual concatenations. Also, this function will
                      correctly step over a Tab position if a substring is longer than the TabSize
                      (again, I didn't check the other's code for this).

                      Rick - MVP

                      Function FillInTabs(ByVa l TextIn, _
                      Optional TabSize As Integer = 8) As String
                      Dim X As Long
                      Dim Position As Long
                      Dim TabOffset As Long
                      Dim TextArray() As String
                      Position = 1
                      TabOffset = 1 + TabSize
                      TextArray = Split(TextIn, vbTab)
                      FillInTabs = Space$(Len(Text In) + _
                      TabOffset * UBound(TextArra y))
                      For X = 0 To UBound(TextArra y)
                      Mid$(FillInTabs , Position) = TextArray(X)
                      Position = Position + Len(TextArray(X ))
                      If Position Mod TabOffset Then
                      Position = TabOffset * _
                      Int((Position + TabOffset) / _
                      TabOffset)
                      End If
                      Next
                      FillInTabs = Trim$(FillInTab s)
                      End Function


                      Comment

                      • Tony Proctor

                        #12
                        Re: converting tabs into spaces?

                        I haven't checked your code Rick, but it looks to be a good efficient
                        method.

                        P.S. I think the TAB offsets would be 1,9,17,25,etc, rather than 1,9,18,...
                        :-)
                        As you can tell from my 2nd posting here, I too find zero-based indexing
                        more natural, and flipping between languages doesn't help the gray hairs.

                        Tony

                        "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
                        news:upK59xIUDH A.1868@TK2MSFTN GP11.phx.gbl...[color=blue][color=green]
                        > > i have a vb6 project, one of the functions is to read in a text file and
                        > > place it into an array
                        > > problem is, when people use the TAB key in the original file that is[/color][/color]
                        read,[color=blue]
                        > i[color=green]
                        > > just get the tab code in the string,
                        > > what i need is for the TAB to literally become that many SPACES ... the
                        > > problem is this
                        > > can vary depending on where exactly the TAB key is hit!
                        > > i have tried
                        > >
                        > > sOrigString = Replace(sOrigSt ring, vbTab, " ")
                        > >
                        > > but this just puts in one space
                        > > of course if someone hits the tab key at the start of the line it will[/color]
                        > jump[color=green]
                        > > the cursor further than if someone hit it a few characters in (if you[/color][/color]
                        know[color=blue][color=green]
                        > > what i mean!)
                        > >
                        > > can anybody help?[/color]
                        >
                        > I've read through the thread and, if I understand where Tab places the[/color]
                        next[color=blue]
                        > print position correctly (an 8 space tab writes at positions 1,9,18,etc.;
                        > otherwise adjust the TabOffset calculation if I'm wrong), wish to offer[/color]
                        this[color=blue]
                        > alternative to the other posts. I didn't check the other's codes, but in a
                        > long string, I'm thinking this may be faster in that it uses a Mid$
                        > statement as opposed to continual concatenations. Also, this function will
                        > correctly step over a Tab position if a substring is longer than the[/color]
                        TabSize[color=blue]
                        > (again, I didn't check the other's code for this).
                        >
                        > Rick - MVP
                        >
                        > Function FillInTabs(ByVa l TextIn, _
                        > Optional TabSize As Integer = 8) As String
                        > Dim X As Long
                        > Dim Position As Long
                        > Dim TabOffset As Long
                        > Dim TextArray() As String
                        > Position = 1
                        > TabOffset = 1 + TabSize
                        > TextArray = Split(TextIn, vbTab)
                        > FillInTabs = Space$(Len(Text In) + _
                        > TabOffset * UBound(TextArra y))
                        > For X = 0 To UBound(TextArra y)
                        > Mid$(FillInTabs , Position) = TextArray(X)
                        > Position = Position + Len(TextArray(X ))
                        > If Position Mod TabOffset Then
                        > Position = TabOffset * _
                        > Int((Position + TabOffset) / _
                        > TabOffset)
                        > End If
                        > Next
                        > FillInTabs = Trim$(FillInTab s)
                        > End Function
                        >
                        >[/color]


                        Comment

                        • Rick Rothstein

                          #13
                          Re: converting tabs into spaces?

                          The code above is incorrect... it places tab positions at 1, 9, 18, 27, etc.
                          instead of at the intervals 1, 9, 15, 25, etc. Thanks to Tony Proctor for
                          pointing this out in another part of this thread. Here is the correct code
                          to use.

                          Function FillInTabs(ByVa l TextIn, _
                          Optional TabSize As Integer = 8) As String
                          Dim X As Long
                          Dim Position As Long
                          Dim TextArray() As String
                          Position = 0
                          TextArray = Split(TextIn, vbTab)
                          FillInTabs = Space$(Len(Text In) + _
                          TabSize * UBound(TextArra y))
                          For X = 0 To UBound(TextArra y)
                          Mid$(FillInTabs , Position + 1) = TextArray(X)
                          Position = Position + Len(TextArray(X ))
                          If Position Mod TabSize Then
                          Position = TabSize * _
                          Int((Position + TabSize) / _
                          TabSize)
                          End If
                          Next
                          FillInTabs = Trim$(FillInTab s)
                          End Function

                          Rick - MVP



                          "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
                          news:upK59xIUDH A.1868@TK2MSFTN GP11.phx.gbl...[color=blue][color=green]
                          > > i have a vb6 project, one of the functions is to read in a text file and
                          > > place it into an array
                          > > problem is, when people use the TAB key in the original file that is[/color][/color]
                          read,[color=blue]
                          > i[color=green]
                          > > just get the tab code in the string,
                          > > what i need is for the TAB to literally become that many SPACES ... the
                          > > problem is this
                          > > can vary depending on where exactly the TAB key is hit!
                          > > i have tried
                          > >
                          > > sOrigString = Replace(sOrigSt ring, vbTab, " ")
                          > >
                          > > but this just puts in one space
                          > > of course if someone hits the tab key at the start of the line it will[/color]
                          > jump[color=green]
                          > > the cursor further than if someone hit it a few characters in (if you[/color][/color]
                          know[color=blue][color=green]
                          > > what i mean!)
                          > >
                          > > can anybody help?[/color]
                          >
                          > I've read through the thread and, if I understand where Tab places the[/color]
                          next[color=blue]
                          > print position correctly (an 8 space tab writes at positions 1,9,18,etc.;
                          > otherwise adjust the TabOffset calculation if I'm wrong), wish to offer[/color]
                          this[color=blue]
                          > alternative to the other posts. I didn't check the other's codes, but in a
                          > long string, I'm thinking this may be faster in that it uses a Mid$
                          > statement as opposed to continual concatenations. Also, this function will
                          > correctly step over a Tab position if a substring is longer than the[/color]
                          TabSize[color=blue]
                          > (again, I didn't check the other's code for this).
                          >
                          > Rick - MVP
                          >
                          > Function FillInTabs(ByVa l TextIn, _
                          > Optional TabSize As Integer = 8) As String
                          > Dim X As Long
                          > Dim Position As Long
                          > Dim TabOffset As Long
                          > Dim TextArray() As String
                          > Position = 1
                          > TabOffset = 1 + TabSize
                          > TextArray = Split(TextIn, vbTab)
                          > FillInTabs = Space$(Len(Text In) + _
                          > TabOffset * UBound(TextArra y))
                          > For X = 0 To UBound(TextArra y)
                          > Mid$(FillInTabs , Position) = TextArray(X)
                          > Position = Position + Len(TextArray(X ))
                          > If Position Mod TabOffset Then
                          > Position = TabOffset * _
                          > Int((Position + TabOffset) / _
                          > TabOffset)
                          > End If
                          > Next
                          > FillInTabs = Trim$(FillInTab s)
                          > End Function
                          >
                          >[/color]


                          Comment

                          • powermanz28
                            New Member
                            • Sep 2005
                            • 1

                            #14
                            I found that this code does not actually work if there is more than one tab in a given line. Reason being that a tab is only viewed as one character so it screws up the math when it gets to the second tab in the line. I modified the code so that after it finds and corrects the first tab, it then rebuilds the line and starts checking for tabs again from the beginning. Here is the code:

                            Function FillInTabs(Text In As String, Optional TabSize As Integer = 8) As String
                            Dim LinePosition As Integer
                            Dim Char(10000) As String
                            Dim NumberOfSpaces As Double
                            Dim TheRestOfTheLin e As String
                            Dim FirstHalf As Integer
                            Dim LineText As String

                            LineText = TextIn

                            10
                            For LinePosition = 1 To Len(LineText)
                            Char(LinePositi on) = Mid(LineText, LinePosition, 1)
                            If Char(LinePositi on) = vbTab Then
                            NumberOfSpaces = TabSize - ((LinePosition - 1) Mod TabSize)
                            Char(LinePositi on) = Space(NumberOfS paces)
                            TheRestOfTheLin e = Mid(LineText, LinePosition + 1, Len(LineText) - LinePosition)
                            LineText = ""
                            For FirstHalf = 1 To LinePosition
                            LineText = LineText + Char(FirstHalf)
                            Next
                            LineText = LineText + TheRestOfTheLin e
                            GoTo 10
                            End If
                            Next

                            FillInTabs = LineText

                            End Function
                            Last edited by powermanz28; Sep 25 '05, 09:39 PM. Reason: changed thread subscription

                            Comment

                            Working...