Simplify Quoted Strings in VBS

In VBS, strings are always wrapped in double quotes:

WScript.Echo("This is a string")

The output from this code is:

This is a string

Now, if you want (or need) to have a string that itself contains double quotes (for example, if you need to refer to a file path), you need to escape the quotes. To make things complicated, the escape character is a double quote! So what you end up with is:

WScript.Echo("This is a string with some ""quoted text"" in it")

The output from this code is:

This is a string with some "quoted text" in it

If the text that is to be quoted is being concatenated in as a variable, it gets even more confusing, as you need 3 sets of double quotes before and after the variable:

qtd_txt = "quoted text"
WScript.Echo("This is a string with some """+qtd_txt+""" in it")
 

Again, the output from this code is:

This is a string with some "quoted text" in it

So what can be done to simplify this confusing sea of quotes?

The following one-line (ignoring the verbose comments!) function can help:


'Function: cleanQuote
'   Replaces a dummy character in a string with the correct number of quotation
'   marks ("). The dummy character should be one that has no other meaning within
'   the string.
'
'Params:
'   str - the source string containing the dummy characters
'   dummy_char - the dummy character to be replaced
'
'Returns:
'   The original string but with all the dummy characters replaced by quotes
'
'Example usage:
'   tst = "this is a ¬quoted string¬"
'   clean = cleanQuote(tst, "¬")
'       'clean equals: "this is a ""quoted string"""
'
'Version:
'   25 Apr 2011 - Paul Walker - Created
'
Function cleanQuote (str, dummy_char)
    cleanQuote = Replace(str, dummy_char, """")
End Function

I think it’s fairly self-explanatory (it should be with so much comment!), but for sake of completeness, this is how you’d use it for the 3rd example above:

qtd_txt = "quoted text"
txt = "This is a string with some ¬"+qtd_txt+"¬ in it"
final_txt = cleanQuote(txt,"¬")
WScript.Echo(final_txt)

'Function: cleanQuote
'   Replaces a dummy character in a string with the correct number of quotation
'   marks ("). The dummy character should be one that has no other meaning within
'   the string.
'
'Params:
'   str - the source string containing the dummy characters
'   dummy_char - the dummy character to be replaced
'
'Returns:
'   The original string but with all the dummy characters replaced by quotes
'
'Example usage:
'   tst = "this is a ¬quoted string¬"
'   clean = cleanQuote(tst, "¬")
'       'clean equals: "this is a ""quoted string"""
'
'Version:
'   25 Apr 2011 - Paul Walker - Created
'
Function cleanQuote (str, dummy_char)
    cleanQuote = Replace(str, dummy_char, """")
End Function

Again, the output from this code is:

This is a string with some "quoted text" in it

While this might look a bit more complicated, it really isn’t. All we’re actually doing is replacing a pair of double quotes (the escape character followed by the quote we’re displaying) with one arbitrary character. This makes our quoted string much easier to read: in the above example, the otherwise meaningless ¬ character means “a double quote will be displayed here”, and our new little function sorts out the mess.

Advertisements

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s