1. Technology

An Example Program for String Processing

A handy utility and an example of VB.NET techniques


Updated July 03, 2008

Programming techniques illustrated in this article:

  • Using the .NET Windows Forms OpenfileDialog and SaveFileDialog
  • Using the new .NET StringBuilder object
  • Using RegEx (Regular Expressions) and its cousin, the Like keyword

Why I wrote this program

In my various other non-VB lives, I often have a need to peer a bit more deeply into EXE and DLL files to try to discover a little more about them. Or maybe I'm just excessively curious. In any case, the information you get from the file Properties, while interesting, isn't enough for me. So, years ago, I wrote a quick utility that would write the character strings that might exist within a file to the screen. (I think I wrote it in QuickBasic at the time.)

Time has passed and I lost that original program. I'm missing it a bit. So I resolved to write it again and this time, offer it for both use and education of About Visual Basic readers.

One note of caution: I really did write this program in just the last few days and this is currently the first version. Note that the version number is 1.0. So there may be a few ... ummm ... oversights in it. If you find one, please send an email and let me know what it is. I plan to update this article, and the downloadable application, to include suggestions and corrections from readers.

Using the Program

The main form for the program consists of four buttons:

  • Select a file to scan
  • Select what to scan for
  • Start the scan
  • Exit

Click Here to display the illustration
Click the Back button on your browser to return

The top button and the "Exit" button are initially the only ones with the Enabled property set to True. As the three steps in the program are completed, each of the top three buttons is enabled in turn to guide the user through the process.

The program is mainly useful for checking the internals of files because, often, text strings will give you a better clue about what the file does. You can check sites like http://www.processlibrary.com/ and http://www.whatisthatfile.com/ but they only identify files by name. This can give you a misleading sense of security. But because messages, constants, and other information exist as snippets of text in files, you can see more of what's going on by simply looking for them. For example, it's not clear at all what the file "d3d8.dll" does. But displaying the text strings in it quickly gives you a better idea.

Click Here to display the illustration
Click the Back button on your browser to return

Using the .NET Windows Forms OpenfileDialog and SaveFileDialog

If you have ever tried to write a foolproof file open or save routine, you will appreciate how much time and effort these tools will save. These are the same dialogs that Windows uses so they're immediately familiar and well tested. Although you can instantiate them in code ...

Dim dialogName As New OpenFileDialog()

... it's easier to just drag them to a form from the Toolbox.

The properties include things like the types of files to open or save (The Filter property - I hard code the type to save in this program as *.txt.) and whether to add a file extension automatically. After a file is opened, another property supplies the name and path as in this from the program ...

FileToScan = OpenFileDialog.FileName

When the file is saved, you can write to the file and save it in the same statment:

If SaveFileDialog.ShowDialog = _
   Windows.Forms.DialogResult.OK Then
   My.Computer.FileSystem.WriteAllText( _
      SaveFileDialog.FileName, _
      sb_all, True)
End If

Using the new .NET StringBuilder object

The program reads characters and tests them one at a time to see if they are "text" (more on that later). One StringBuilder object is used to accumulate text as it's being read until a non-text character is found. If the string fragment accumulated is longer than the minimum set, it's written to the screen and to a different StringBuilder object that is accumulating all of the string fragments. When the entire file has been processed, the accumulated fragments that were found are saved using the SaveFileDialog control as shown above.

Since the StringBuilder object is new, an entire article was devoted to it recently: StringBuilder ... A New Object in .NET.

Using RegEx (Regular Expressions) and its cousin, the Like keyword

There are three places in the program where strings must be tested. First, "text" is defined as ...

If (TestChar > 47 And TestChar < 58) Or _
   (TestChar > 64 And TestChar < 91) Or _
   (TestChar > 96 And TestChar < 123) Or _
   TestChar = 32 Then

These are the ASCII decimal equivalents of numbers, lower case letters, upper case letters, and the space character. I chose to do it this way because only a single character was being tested and because it's easier to see what's going on. And also to contrast this kind of string matching with RegEx and Like.

Another part of the program needs to determine whether a positive whole integer is used to specify the minimum length of a string fragment. I'm sure there are other ways to do it, but I used RegEx "light" - the Like operator - to determine this.

If Len(ScanStringLength.Text) < 1 Or _
   ScanStringLength.Text Like "*[!0-9]*" Then

Like is a little known operator that has most of the advantages of the full RegEx without as much complexity. But it can still be confusing. For both RegEx and Like, use the Immediate window to test your code quickly as shown below.

Click Here to display the illustration
Click the Back button on your browser to return

I also use the real RegEx, in a very simple way, to count the number of occurances of a specific string in the file being scanned.

Dim parser As New Regex(ParmsDialog.ScanString.Text)
Dim StringCount As Integer = _

For a more complete explanation of RegEx: Regular Expressions in VB.NET

To download the complete application (written for VB.NET 2008 and Framework 3.5) -- Click Here.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. An Example Program for String Processing

©2014 About.com. All rights reserved.