I have doing a little too much work with legacy code (Visual Basic 6) recently, I was actually a better than good VB6 developer at one point but suddenly my mind cannot get around the fact that Strings in VB6 just simply do not have all the trappings (.Length, .Trim, .Remove, .Split etc) of their C# equivalents.

Anyway, I was tackling an issue that required me to remove all the unprintable characters (specifically the ASCII range 0-31) from a string in VB6 and this is what I came up with:

Dim lIndex as Integer
Dim sParseThis as String

sParse = GetDataWithWierdCharacters

For lIndex = 0 to 31
     sParse = Replace(sParse, Chr(lIndex), "")
Next

When I started thinking about this code in terms of C# my immediate desire was to make sure I am taking advantage of the glorious spackle that is the .NET Framework. Simply put I did not want to copy the above code, line for line in C#. So I thought I could take advantage of regular expressions.

string sParse;
sParse = GetDataWithWierdCharacters();
sParse = Regex.Replace(sParse, "[\x01-\x1F]", "");

Much better!



Comment Section

Comments are closed.