469,280 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

converting tabs into spaces?

p s
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(sOrigString, 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
Jul 17 '05 #1
13 11152
On Tue, 22 Jul 2003 15:15:07 GMT, "p s" <pe***********@hotmail.com>
wrote:
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(sOrigString, 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


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(sOrigString, vbTab, string(nTabSize, " "))

where tabsize is set to 4 or 8 or whatever you tabsize is.
steve
Jul 17 '05 #2
p s

"steve hotmail.com>" <stesrc@<remove.this.antispam.bit> wrote in message
news:vh********************************@4ax.com...
On Tue, 22 Jul 2003 15:15:07 GMT, "p s" <pe***********@hotmail.com>
wrote:
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, ijust 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(sOrigString, vbTab, " ")

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

can anybody help?

thanks
pete


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(sOrigString, vbTab, string(nTabSize, " "))

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

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
Jul 17 '05 #3
On Tue, 22 Jul 2003 15:43:44 GMT, "p s" <pe***********@hotmail.com>
wrote:

"steve hotmail.com>" <stesrc@<remove.this.antispam.bit> wrote in message
news:vh********************************@4ax.com.. .
On Tue, 22 Jul 2003 15:15:07 GMT, "p s" <pe***********@hotmail.com>
wrote:
>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 isread, 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(sOrigString, vbTab, " ")
>
>but this just puts in one space
>of course if someone hits the tab key at the start of the line it willjump >the cursor further than if someone hit it a few characters in (if youknow >what i mean!)
>
>can anybody help?
>
>thanks
>pete
>


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(sOrigString, vbTab, string(nTabSize, " "))

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

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

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
Jul 17 '05 #4
p s
> 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


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
Jul 17 '05 #5
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(iCh)-iCh)
Tony

"p s" <pe***********@hotmail.com> wrote in message
news:%x*******************@news-text.cableinet.net...
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(sOrigString, 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

Jul 17 '05 #6
Al
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" <pe***********@hotmail.com> wrote in message
news:%x*******************@news-text.cableinet.net...
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(sOrigString, 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

Jul 17 '05 #7
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@aimtechnology_NOSPAM.com> wrote in message
news:e0**************@tk2msftngp13.phx.gbl...
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(iCh)-iCh)
Tony

"p s" <pe***********@hotmail.com> wrote in message
news:%x*******************@news-text.cableinet.net...
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(sOrigString, 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


Jul 17 '05 #8
p s
thanks tony, same principle as norms code, exactly what i needed

"Tony Proctor" <tony_proctor@aimtechnology_NOSPAM.com> wrote in message
news:OT**************@tk2msftngp13.phx.gbl...
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@aimtechnology_NOSPAM.com> wrote in message
news:e0**************@tk2msftngp13.phx.gbl...
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(iCh)-iCh)
Tony

"p s" <pe***********@hotmail.com> wrote in message
news:%x*******************@news-text.cableinet.net...
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(sOrigString, 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



Jul 17 '05 #9
p s
cheers al
problem is my lines are varying ... they are not uniform as in your example
thanks again
pete

"Al" <al*****@mashispano.com> wrote in message
news:un*************@TK2MSFTNGP12.phx.gbl...
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" <pe***********@hotmail.com> wrote in message
news:%x*******************@news-text.cableinet.net...
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(sOrigString, 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


Jul 17 '05 #10
> 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(sOrigString, 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?


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(ByVal 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(TextIn) + _
TabOffset * UBound(TextArray))
For X = 0 To UBound(TextArray)
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$(FillInTabs)
End Function
Jul 17 '05 #11
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" <ri************@NOSPAMcomcast.net> wrote in message
news:up**************@TK2MSFTNGP11.phx.gbl...
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(sOrigString, 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?


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(ByVal 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(TextIn) + _
TabOffset * UBound(TextArray))
For X = 0 To UBound(TextArray)
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$(FillInTabs)
End Function

Jul 17 '05 #12
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(ByVal 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(TextIn) + _
TabSize * UBound(TextArray))
For X = 0 To UBound(TextArray)
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$(FillInTabs)
End Function

Rick - MVP

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:up**************@TK2MSFTNGP11.phx.gbl...
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(sOrigString, 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?


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(ByVal 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(TextIn) + _
TabOffset * UBound(TextArray))
For X = 0 To UBound(TextArray)
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$(FillInTabs)
End Function

Jul 17 '05 #13
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(TextIn As String, Optional TabSize As Integer = 8) As String
Dim LinePosition As Integer
Dim Char(10000) As String
Dim NumberOfSpaces As Double
Dim TheRestOfTheLine As String
Dim FirstHalf As Integer
Dim LineText As String

LineText = TextIn

10
For LinePosition = 1 To Len(LineText)
Char(LinePosition) = Mid(LineText, LinePosition, 1)
If Char(LinePosition) = vbTab Then
NumberOfSpaces = TabSize - ((LinePosition - 1) Mod TabSize)
Char(LinePosition) = Space(NumberOfSpaces)
TheRestOfTheLine = Mid(LineText, LinePosition + 1, Len(LineText) - LinePosition)
LineText = ""
For FirstHalf = 1 To LinePosition
LineText = LineText + Char(FirstHalf)
Next
LineText = LineText + TheRestOfTheLine
GoTo 10
End If
Next

FillInTabs = LineText

End Function
Sep 25 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan Jacobson | last post: by
17 posts views Thread by bearophileHUGS | last post: by
1 post views Thread by Kevin Auch | last post: by
135 posts views Thread by Xah Lee | last post: by
2 posts views Thread by Laurence | last post: by
15 posts views Thread by Tom Plunket | last post: by
35 posts views Thread by Ben | last post: by
16 posts views Thread by Alan Isaac | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.