Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript: What is the simplest way to format a string?

I have the following format: Value1 is {0} and Value2 is {1}.

I need to replace the numbers in the brackets with strings. This is easily done in most languages using string.Format or something along those lines. How can I do this using only vbscript?

I've tried:

Replace (strFormat, "{0}", value1)  
Replace (strFormat, "{1}", value2)

It does not work. Any solutions?

like image 326
Paxenos Avatar asked Dec 03 '09 15:12

Paxenos


People also ask

What is a string format VB?

In visual basic, the string Format method is useful to insert the value of variable or an object or expression into another string. By using the string Format method, we can replace the format items in the specified string with the string representation of specified objects.

What is the format of string?

String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text. As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.

What is string function in VBScript?

❮ Complete VBScript Reference. The String function returns a string that contains a repeating character of a specified length.


4 Answers

Replace (strFormat, "{0}", value1)

Based on your code snip, I'm guessing you believe Replace mutates strFormat directly. It doesn't work like that; You assign the result to the original variable like this:

strFormat = Replace (strFormat, "{0}", value1)

You can also assign to another variable to store the changed results, like this:

strFormat2 = Replace (strFormat, "{0}", value1)
like image 94
JasDev Avatar answered Sep 18 '22 16:09

JasDev


I wanted something similar and didn't like any of these answers as they meant multiple lines for each value (Ignoring Beaner's answer is for the wrong language!) so I created the following:

Public Function StrFormat(FormatString, Arguments())
    Dim Value, CurArgNum

    StrFormat = FormatString

    CurArgNum = 0
    For Each Value In Arguments
        StrFormat = Replace(StrFormat, "{" & CurArgNum & "}", Value)
        CurArgNum = CurArgNum + 1
    Next
End Function

You can use the following then (note that you need to add "Array()" around your variables):

formatString = "Test '{0}', '{2}', '{1}' and {0} again!"
Response.Write StrFormat(formatString, Array(1, 2, "three", "Unused"))
Response.Write StrFormat(formatString, Array(4, 5, "six", "Unused"))

Which will output what you expect:

Test '1', 'three', '2' and 1 again!
Test '4', 'six', '5' and 4 again!

Hope this feels a bit more natural for people from other languages.

like image 23
GazB Avatar answered Sep 20 '22 16:09

GazB


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.

like image 31
Ekkehard.Horner Avatar answered Sep 22 '22 16:09

Ekkehard.Horner


Here's a nice little function that works something like the .NET string.Format function. I did this quickly so adding err handling is up to you. I did this in VB6 and added a reference to Microsoft VBScript Regular Expressions 5.5

Public Function StringFormat(ByVal SourceString As String, ParamArray Arguments() As Variant) As String
   Dim objRegEx As RegExp  ' regular expression object
   Dim objMatch As Match   ' regular expression match object
   Dim strReturn As String ' the string that will be returned

   Set objRegEx = New RegExp
   objRegEx.Global = True
   objRegEx.Pattern = "(\{)(\d)(\})"

   strReturn = SourceString
   For Each objMatch In objRegEx.Execute(SourceString)
      strReturn = Replace(strReturn, objMatch.Value, Arguments(CInt(objMatch.SubMatches(1))))
   Next objMatch

   StringFormat = strReturn

End Function

Example:

StringFormat("Hello {0}. I'd like you to meet {1}. They both work for {2}. {0} has worked for {2} for 15 years.", "Bruce", "Chris", "Kyle")

Returns:

Hello Bruce. I'd like you to meet Chris. They both work for Kyle. Bruce has worked for Kyle for 15 years.

like image 30
jac Avatar answered Sep 19 '22 16:09

jac