Code Focused

Overcoming Escape Sequence Envy in Visual Basic and C#

C# might be more elegant with escape sequences, but that doesn't mean Visual Basic is weaker in this area.

Sometimes you just need to escape. I'm speaking, of course, about escape sequences in strings. Standard C# strings have them in spades: \t for tabs, \r\n for line breaks, \u1234 for who-knows-what random Unicode character. Meanwhile, Visual Basic developers spend their days concatenating one special character after another onto their boring strings.

Consider something as simple as a notification that includes multi-line, tab-indented bullet items. C# allows the special bullet characters and the less-special line breaks and tabs to be incorporated into the strings themselves. But in Visual Basic, such treats must be attached by brute force:

// ----- Lean and mean C# version
MessageBox.Show(
  "To apply changes:\r\n" +
  "\u2022\tSave your work\r\n" +
  "\u2022\tRestart the program.");

' ----- Plodding Visual Basic version
MessageBox.Show(
  "To apply changes:" & vbCrLf &
  ChrW(8226) & vbTab & "Save your work" & vbCrLf &
  ChrW(8226) & vbTab & "Restart the program.")

Such Visual Basic code works just fine, of course. But it lacks the fluidity of the complex strings found in C#. Fortunately, there are ways to improve string processing in Visual Basic, up to and including the use of true escape sequences.

One way to reduce the level of concatenation is to use the String.Format method, integrating the special characters as if they were data values:

MessageBox.Show(String.Format(
  "To apply changes:{0}" &
  "{1}{2}Save your work{0}" &
  "{1}{2}Restart the program.",
  vbCrLf, ChrW(8226), vbTab))

Using String.Format in this way makes the core content much cleaner, but the special characters are still external to the content. Interpolated strings, new in Visual Basic 2015, allow these characters to be brought directly into the strings themselves:

' ----- Only in Visual Basic 2015 and beyond
Dim bulletPoint As Char = ChrW(8226)
MessageBox.Show(
  $"To apply changes:{vbCrLf}" &
  $"{bulletPoint}{vbTab}Save your work{vbCrLf}" &
  $"{bulletPoint}{vbTab}Restart the program.")

If readability is your goal, this is a great option. But there is still the issue of escape sequence envy, which doesn't go away easily. Fortunately, Visual Basic can handle strings with true escape characters, albeit with the help of the .NET regular expression library. The Regex.Unescape method accepts a string with backslash-powered escape codes, and spits out string content with the sequences properly morphed into their intended characters:

' Assumes: "Imports System.Text.RegularExpressions"
MessageBox.Show(Regex.Unescape(
  "To apply changes:\r\n" &
  "\u2022\tSave your work\r\n" &
  "\u2022\tRestart the program."))

Using Regex.Unescape in this way doesn't provide an exact match for what's available in C# strings. In fact, it provides more, because the Unescape method processes one escape code not found in C#: The \c sequence. When followed by a letter of the alphabet, this sequence inserts that "control code" into the string. For example, \cI inserts a Control-I, better known as the tab character, into the result:

MessageBox.Show(Regex.Unescape(
  "To apply changes:\r\n" &
  "\u2022\cISave your work\r\n" &
  "\u2022\cIRestart the program."))

Now which language has escape-sequence envy?

About the Author

Tim Patrick has spent more than thirty years as a software architect and developer. His two most recent books on .NET development -- Start-to-Finish Visual C# 2015, and Start-to-Finish Visual Basic 2015 -- are available from http://owanipress.com. He blogs regularly at http://wellreadman.com.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube