Como ninguno de las respuestas hasta ahora aborda el problema de formatear (en contraposición a la interpolación de cadenas/empalme en cadenas):
Este sencillo Clase:
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
arnés. Formato de red para VBScript a través de COM. Ahora se puede hacer:
-------- 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|
Si usted está interesado en el script de prueba/demo que hacer algunos experimentos de su propia:
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
Para obtener información sobre el formato en .NET, y empezar con StringBuilder.AppendFormat Method (String, Object)Formatting Types.
Consulte here y here para obtener ideas que incluyan (no Copie & Pegar) dicha Clase en su secuencia de comandos.
Hehe, gracias. Siempre es algo simple. – Paxenos
no necesita _una otra variable, pero solo para actualizar la cadena original. –