Wie keine der Antworten Adressen bisher das Problem der Formatierung (wie zu interpoliert/Splicing-Strings in Strings entgegengesetzt):
Diese einfache Klasse:
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
Geschirr. NET-Formatierung für VBScript über COM. Jetzt können Sie tun:
-------- 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|
Wenn Sie Interesse an dem Test/Demo-Skript sind einige Experimente Ihrer eigenen zu tun:
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
Um zu erfahren, in .NET Formatierung, beginnen Sie mit StringBuilder.AppendFormat Method (String, Object) und Formatting Types.
Siehe here und here für Ideen zu enthalten (nicht kopieren & einfügen) eine solche Klasse in Ihr Skript.
Hehe, Danke. Es ist immer etwas Einfaches. – Paxenos
er benötigt keine _andere_ Variable, sondern nur um die ursprüngliche Zeichenkette zu aktualisieren. –