Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript - String Formatting (output in to columns)

Status
Not open for further replies.

humbletech99

Programmer
Nov 22, 2005
155
0
0
GB
I notice there are doesn't seem to be any proper string formatting in VBScript in order to specify how output strings should come out.

I am trying to align output into clear columns, regardless of the length of the various column inputs. Hence I need a way of stating that a column should be X characters across including spaces in length regardless ("%-10s" type stuff if any of your know string formatting from any other languages)

I suspect that I will have to write a function to do this...

What do you do for string formatting in VBS?
 
Yes you will have to write your own functions for formatting.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
This might be one way....

Code:
Option Explicit

Dim arrTemp : arrTemp = Array("something", "something else", "another thing", "this thing", "is it time to go home yet")
Dim strTemp
' output not in nice columns
For Each strTemp In arrTemp 
	WScript.Echo strTemp & " " & Now
Next

WScript.Echo String(40, "=")
' try to get the longest string length
Dim intSpace : intSpace = 0
For Each strTemp In arrTemp
	If Len(strTemp) > intSpace Then intSpace = Len(strTemp)
Next

' add some extra spaces
intSpace = intSpace + 5
' loop through strings putting in the necessary spaces
For Each strTemp In arrTemp
	WScript.Echo strTemp & Space(intSpace - Len(strTemp)) & Now
Next

WScript.Echo String(40, "=")

'or if you know the longest string length will be less than a certain number

intSpace = 32
For Each strTemp In arrTemp
	WScript.Echo strTemp & Space(intSpace - Len(strTemp)) & Now
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
If all you are wanting to do is format the data into fixed length strings try something like this:

'To Left Justify:
MsgBox Format("TEST", "!" & String(50, "@"))
'To Right Justify:
MsgBox Format("TEST", String(50, "@"))

Swi
 
I tried the right justify one for example but couldn't make it work:
Code:
MsgBox Format("test", String(50, "@"))
Microsoft VBScript runtime error: Type mismatch: 'Format'
I don't understand Format, where did you get this function from? It must be a custom function, not an in-built one?

I think this is a VB function, you'd have to write a com object to be able to import it... and hence not portable either.
 
Yes, Format()/Format$() are VB functions not present in VBScript. You'd need to write some sort of function if you need this generally.

Several specialized formatting functions are available for non-String types.

I suspect that the lack of an obvious answer means this is a rare requirement for scripting. Otherwise we'd already have a standard component with the capability or you'd have seen multiple responses with various alternatives or even a FAQ entry here.
 
Sorry, bad example. I thought for some reason it did exist in VBScript.

Swi
 
I've just written a very simple padding function to take care of it. In fact, I thought of something much simpler than the examples I've seen:
Code:
function pad(strText)
    strText = strText + Space(50)
    strText = Left(strText, 50)
    pad = strText
end function

strVar = pad("something")
 
Well that's deadly. [tt]strText[/tt] is passed ByRef by default so you've messed it up by adding spaces to it.

Faster anyway to just use:
Code:
Function Pad(ByRef Text, ByVal Length)
  Pad = Left(Text & Space(Length), Length)
End Function
 
err I meant for that + to be a &... but it works anyway, that was a quick write off the top of my head just to illustrate...

but yes your approach is even slightly better. Thanks
 
Well my concern wasn't the + as much as the fact that you were assigning back to the strText parameter.
 
I didn't realize that it was being passed by reference, I thought it would have been passed by value...

In any event, it doesn't really matter, it still works... but thank you very much for pointing that out, it might save me a bug in future.
 
oh yes I remember that vbs does this now, it's how you use backpassing of parameters.... an ugly practice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top