'vbscript files with spaces and tabs
Got a file that I want to separate and put in columns. Look like some of the fields in file are separated by spaces and other by tabs. I want to format so the the name and address and etc are separated in columns.
enter code here
strStarter = "RT 29 MONTPELIER VA 23100 23100"
intStarter = Len(strStarter)
For i = intStarter to 2 Step -1
strChars = Space(i)
strStarter = Replace(strStarter, strChars, " ")
strStarter1 = Replace(strStarter, vbTab, " " )
Next
arrStarter = Split(strStarter, " ")
arrStarter2= Split(strStarter1,vbTab)
For Each strUnit in arrStarter
Wscript.Echo strUnit
Next
Solution 1:[1]
I think that you have a tab-delimited file, perhaps with sequences of blanks in some fields. In that case you should
- Split on vbTab to get the fields
- normalize blanks in all/some of the fields
To normalize spaces you can use Trim() for leading/trailing blanks and a RegExp.Replace with "+" or " +" as pattern and " " as replacement.
(have a look here)
Solution 2:[2]
I made you a Ruby-like split function to split a string based on a Regular Expression pattern
'the following string contains a tab
strStarter = "RT 29 MONTPELIER BEFORETAB AFTERTAB VA 23100 23100"
Function rsplit(string, pattern)
Dim r
Set r = New RegExp
r.Global = True
r.Pattern = "[^"&pattern&"]+"
Set rsplit = r.Execute(string)
End Function
For each unit in rsplit(strStarter, " |\t")
wscript.echo unit
Next
Gives
RT
29
MONTPELIER
BEFORETAB
AFTERTAB
VA
23100
23100
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | peter |