Tiptoeing Towards Twitter?

I’ve haven’t seen any need for me to have a Twitter account so far. There’s only ever been a couple of people that I’ve followed, and I’ve done that by subscribing to the RSS feeds.

Tonight, I was having trouble connecting to Apple’s App Store from my iPod Touch and I wondered if it was just me. I’m not sure why (because I’ve never done it before), but the first place I thought to look was Twitter! Sure enough, as soon as I searched for “App Store”, I found plenty of reports that other people were having similar difficulties.

I must admit to being quite impressed; I’d never looked at Twitter enough to realise it was so searchable. Will I search it again in future? Almost certainly. Will I create my own account and start tweeting? Unlikely.

Advertisements

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.