Visual Basic and Escaped Characters

I really like C#.  I have programmed in it personally, doing the games development course in it, also doing it commercially having developed web sites and updating tools and utilities written in C#.  But despite my first programming languages being x86 Assembly and Pascal I feel that my heart will lie with Visual Basic.  Since my best friend Phippsy, showed me VB3 back in the days, it has been a memorable journey from baby steps to the more advanced by claiming my MCSD in VB6.

So where am I going in this nostalgic post.  Well it is about strings and escaped characters within the string.  C, C++ and C# and I believe Java and others have enjoyed the maintainability of using escaped characters within a string to denote an unprintable character.  The most common of these is the \n (new line).

I have found that despite the many changes in the language over the years, the implementation of classes, expanding OO to not quite OO and then the full implementation of OO with .NET.  Then the progressions of the language within .NET even seeing some features in VB getting adopted by C#.  But escaped characters in the string is still not there.  Not sure why, I can only guess and I am not wanting to.

So, where does that leave us in the world of VB.  Continuing to use the concatenation operator &.  Or trying something different, which is what I am going to describe here.  There are 2 methods that are usable and very viable as an option within VB.

Traditional Method

Dim strMessage As String
strMessage = "This is a message." & vbCrLf & "Separated by a new line."

This would be a common view for many VB programmers.

String.Format Method

Well this is the first of the different ways.  Now there is nothing revolutionary about these methods, other than they might not be ways you have thought of and continue to use the above traditional method to add new lines and other non-printable characters to a string.


Dim strMessage As String
 strMessage = String.Format("This is a message.{0}Separated by a new line.", vbCrLf)

Please disregard the fact that I would put this all onto one line.  But I was trying to highlight the differences in each.  It is longer, but to me, this string, that is the characters enclosed by the quotes is more readable, this increases substantially when there is more than one of these.

See String.Format

Regex.Unescape Method

This one seems a little obscure, but works in a manner to which C users (all forms) are accustom to and if you have come from that environment into VB, this might be the preferred method.

There is one downside to this, the importing of a new namespace.  But fear not, this will not bloat things, or slow your code to snail like proportions but it is something to be aware of is this is new code that will be loaded and JITed which does take a hit.  I will include the imports statement into the code, just for completeness.

Imports System.Text.RegularExpressions

Dim strMessage As String
strMessage = RegEx.Unescape("This is a message.\nSeparated by a new line.")

There is it, the \n just like in C.  And this is what this unescape method does, it “Unescapes any escaped characters in the input string.”

See Regex.Unescape

C# Just for Comparison

When one looks at the three, it is easy to see the virtues in using escaped characters within the string.  Though, I am in still two minds about using them.  Why, well in so many ways, it is not something I am used to.  Having developed possibly hundreds of thousands of lines of code in VB, old habits are hard to break.  But, are there benefits to the new methods, maintenance.  Having new people coming in, looking at the code and understanding what it is doing, especially if they have come from a C environment previously.


string strMessage;
 strMessage = "This is a message.\nSeparated by a new line.";

P.S.  Please forgive the code posting.  I am using Windows Live Writer, and I have been trying different code plug-ins for it to enable me to enter my code into the writer and post it on the blog.  Provide some differences in the look of it to signify that it is code, but also to make these elements easier to read.

There are a few of them, that makes the code look great in the Preview page, but when it comes time to post it, somehow it is mangled and I don’t have the time to work out why it has failed to make them look the same.

P.P.S.  I have updated this post and I have fixed the code blocks.  I will now go back and update all of my previous postings that have code in them.  Sorry for this.

Advertisements

Posted on August 6, 2012, in Article, Development, Tips and Tricks and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

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

%d bloggers like this: