473,797 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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
Jul 17 '05
13 11655
> 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?


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
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******** ******@TK2MSFTN GP11.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(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?


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

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(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" <ri************ @NOSPAMcomcast. net> wrote in message
news:up******** ******@TK2MSFTN GP11.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(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?


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

Jul 17 '05 #13
powermanz28
1 New Member
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
Sep 25 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3435
by: Dan Jacobson | last post by:
In wdg-html-reference/html40/block/pre.html there is no mention about how ASCII tabs (^I) are to be rendered. I suppose all bets are off? Wait, my docs go thru html-tidy first, and it says tab-size: number Sets the number of columns between successive tab stops. The default is 4. It is used to map tabs to spaces when reading files. Tidy never outputs files with tabs. However, in emacs, tab-width's value is 8. Solution:
17
2206
by: bearophileHUGS | last post by:
Hello, I know this topic was discussed a *lot* in the past, sorry if it bores you... >From the Daily Python-URL I've seen this interesting Floating Point Benchmark: http://www.fourmilab.ch/fourmilog/archives/2005-08/000567.html This is the source pack: http://www.fourmilab.ch/fbench/fbench.zip
1
2045
by: Kevin Auch | last post by:
Hi, I'm working on a little application which analyse source code file from VS.NET. But I'm in front of a big problem. The source generated by VS.NET is automatically "indented", but when I analyse these lines I can't remove the tabs or the spaces at the beginning of the line because it's not spaces or tabs. I tried to change the encoding of my StreamReader but it's always the same...
135
7534
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
2
2318
by: Laurence | last post by:
In VS.2005, using VB.NET My editor is behaving strangely. If I press the tab key, the line moves over the appropriate number of spaces. When I click on another line, the line that I had tab'd on moves over more spaces. I have changed the tab and insert, but it doesn't seem to work.
15
2929
by: Tom Plunket | last post by:
The documentation for dedent says, "Remove any whitespace than can be uniformly removed from the left of every line in `text`", yet I'm finding that it's also modifying the '\t' characters, which is highly undesirable in my application. Is there any way to stop it from doing this, or alternatively, to put those tabs back in? I see that TextWrap and its member functions take an 'expand_tabs' kwarg, but dedent unfortunately does not. ...
35
2732
by: Ben | last post by:
Hi, I have a python script on a unix system that runs fine. I have a python script on a windows system that runs fine. Both use tabs to indent sections of the code. I now want to run them on the same system, actually in the same script by combining bits and pieces. But whatever I try my windows tabs get converted to spaces when I transfer it to the unix system and the interpreter complains that the indentation style is not consistent...
16
10174
by: Alan Isaac | last post by:
I am brand new to pylint. As a tab user, I want the tabs warning turned off. How? Larger question: where is the config file format documented? Thanks, Alan Isaac
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.