I have run across dozens of algoritms for this, but I cannot for the life of me get any to work properly
I do not wish to simply replace a [tab] with [x spaces], I need to EXPAND the tab to the next tab stop
so given
s="xxxxx^|xxxxxx^|"
t="xxxxx^|xxxxx^^|"
and a tabstop size of 8 (with “^” representing the tab and “.” a space)
I want to end up with
where the 1st [tab] in each string would expand to 3 space, moving the “|” to column 9
and the 2nd tab in the first string expanding to full 8 spaces
and the 2nd tab in the second string expanding to only 1 space, and the 2nd one to 8 spaces
both moving the 2nd “|” to column 25
most of the algoritms are very similar to this
dim nb as integer // number of blanks to add
dim c as string
dim i as integer
dim t as string
dim col as integer
col=1
for i=1 to len(s)
c=mid(s,i,1)
if c=chrb(9) then
nb=tabsize-(col % tabsize)
while nb>0
col=col+1
nb=nb-1
t=t+" "
wend
else
t=t+c
end if
next i
return t
and while on the subject, I will need the opposite function… to restore the tabs as well
I don’t think you need to process the string character by character. Rather you need to find each tab and insert the correct number of space to get to the next tab stop.
I would use CountFieldsb-1 to get the number of tabs.
Loop through the number of tabs and find the tabs position.
Add the number of spaces to get to the next tab stop (tabs position in string mod 8)
Function ExpandTabs(s as string) As String
dim spaces,tab,output As string
dim columnWidth,i As integer
//Fixed constants
spaces=" "
tab=chr(9)
columnWidth=8
// Insert spaces
output=NthField(s,tab,1)
for i=2 to countfields(s,tab)
output=output+Left(spaces,columnWidth- ((len(output)+1)mod columnWidth))+nthfield(s,tab,i)
next
return output
End Function