As none of the answers so far addresses the problem of formatting (as opposed
to interpolating/splicing strings into strings):
This simple Class:
Class cFormat
Private m_oSB
Private Sub Class_Initialize()
Set m_oSB = CreateObject("System.Text.StringBuilder")
End Sub ' Class_Initialize
Public Function formatOne(sFmt, vElm)
m_oSB.AppendFormat sFmt, vElm
formatOne = m_oSB.ToString()
m_oSB.Length = 0
End Function ' formatOne
Public Function formatArray(sFmt, aElms)
m_oSB.AppendFormat_4 sFmt, (aElms)
formatArray = m_oSB.ToString()
m_oSB.Length = 0
End Function ' formatArray
End Class ' cFormat
harness .NET formatting for VBScript via COM. Now you can do:
-------- Interpolation
Use |Value1 is {0} and Value2 is {1}.|
to get |Value1 is zero and Value2 is one.|
from |zero one|
Use |{0} x 2 => {0}{0}|
to get |once x 2 => onceonce|
from |once|
-------- Cherrypicking
Use |{6,4}: [{0}, {2}, {4}]|
to get |even: [0, 2, 4]|
from |0 1 2 3 4 5 even odd|
Use |{7,4}: [{5}, {3}, {1}]|
to get | odd: [5, 3, 1]|
from |0 1 2 3 4 5 even odd|
-------- Conversions
Use ||{0:D}| |{0:X}| |{0:N3}| |{0:P2}| (german locale!)|
to get ||123| |7B| |123,000| |12.300,00%| (german locale!)|
from |123|
Use ||{0}| |{0:U}| |{0:u}||
to get ||29.06.2012 14:50:30| |Freitag, 29. Juni 2012 12:50:30| |2012-06-29 14:50:30Z||
from |29.06.2012 14:50:30|
Use ||{0}| |{0:E1}| |{0:N1}| |{0:N2}| |{0:N3}||
to get ||1234,56| |1,2E+003| |1.234,6| |1.234,56| |1.234,560||
from |1234,56|
-------- Alignment
Use ||{0,1:D}| |{0,2:D}| |{0,-2:D}| |{0,5:D}| |{0,-5:D}||
to get ||12| |12| |12| | 12| |12 ||
from |12|
If you are interested in the test/demo script to do some experiments
of your own:
Option Explicit
' Class cFormat ...
Dim oFormat : Set oFormat = New cFormat
Dim aTests : aTests = Array( _
Array("Interpolation" _
, Array( _
Array(True, "Value1 is {0} and Value2 is {1}.", Array("zero", "one")) _
, Array(False, "{0} x 2 => {0}{0}" , "once" ) _
} _
) _
, Array("Cherrypicking" _
, Array( _
Array(True , "{6,4}: [{0}, {2}, {4}]", Array(0, 1, 2, 3, 4, 5, "even", "odd")) _
, Array(True , "{7,4}: [{5}, {3}, {1}]", Array(0, 1, 2, 3, 4, 5, "even", "odd")) _
} _
) _
, Array("Conversions" _
, Array( _
Array(False, "|{0:D}| |{0:X}| |{0:N3}| |{0:P2}| (german locale!)", 123 ) _
, Array(False, "|{0}| |{0:U}| |{0:u}|" , Now ) _
, Array(False, "|{0}| |{0:E1}| |{0:N1}| |{0:N2}| |{0:N3}|" , 1234.56 ) _
} _
) _
, Array("Alignment" _
, Array( _
Array(False, "|{0,1:D}| |{0,2:D}| |{0,-2:D}| |{0,5:D}| |{0,-5:D}|", 12 ) _
} _
) _
)
Dim sFormat : sFormat = "Use |{0}|{3}to get |{1}|{3}from |{2}|{3}"
Dim aData : aData = Array(0, 1, 2, vbCrLf)
Dim aTest
For Each aTest In aTests
WScript.Echo "--------", aTest(0)
Dim aSample
For Each aSample In aTest(1)
aData(0) = aSample(1)
If aSample(0) Then
aData(1) = oFormat.formatArray(aSample(1), aSample(2))
aData(2) = Join(aSample(2))
Else
aData(1) = oFormat.formatOne( aSample(1), aSample(2))
aData(2) = aSample(2)
End If
WScript.Echo oFormat.formatArray(sFormat, aData)
Next
WScript.Echo
Next
To learn about formatting in .NET, start with StringBuilder.AppendFormat Method (String, Object) and Formatting Types.
See here and here for ideas to include (not Copy&Paste) such a Class into your script.