1. Technology

The Filter Function

String Processing in VB.NET


Smiling businesswoman working at laptop in office
Paul Bradbury/OJO Images/Getty Images
Updated June 09, 2014

Strings may be the second most useful "type" in VB.NET. (Integers are probably the first.) So there is a lot of useful string processing built into VB.NET. The Filter function is one and this article is a fast explanation showing how to use it.

Some previous articles at About Visual Basic showing how to use strings ...

-> Strings Are Just Different - An introduction to just what 'type' of object that 'String' really is in VB.NET.
-> StringBuilder ... A New Object in .NET - A more efficient way to join strings together.
-> Formatting Strings, Numbers, and Other Objects - Making strings look the way you want them to look.
-> Amazing Splits - This is an oldie, but a goodie about one of the string manipulation functions.

A String is basically an array of characters ... but .NET gives you the ability to refer to this array as though it was a single object. In spite of this, most of the code that actually processes strings will break them apart and reassemble them into different strings. That's why the last article - the "oldie, but a goodie" article about the Split function has been so popular. In a way, this article about the Filter function is a companion to that one since it can be used for similar tasks.

The syntax of Filter is deceptively simple:

Function Filter(
   ByVal Source() As { Object | String },
   ByVal Match As String,
   Optional ByVal Include As Boolean = True,
   Optional ByVal Compare As CompareMethod = CompareMethod.Binary
)  As String()

The key is Source(). This must be a one dimensional array of strings. In other words, a String type won't work. The Microsoft documentation of Filter is really inadequate because it fails to explore the only difficult part of using it: the difference between String arrays and String objects. This is explored in the first article referenced earlier: Strings Are Just Different. To introduce the problem, consider this code:

Dim TestString As String =
    "How many sheets could a sheet splitter split"
TestString = Filter(TestString, "split")

This code fails with the compiler error, "Overload resolution failed ...". (There's more, but it doesn't help.) The problem is that TestString isn't a String array, it's a String object.

Similarly, the code below fails with a slightly different but also confusing message. (Note that the only change is to add parentheses to TestString so it will be an array.)

Dim TestString() As String =
    "How many sheets could a sheet splitter split"
TestString = Filter(TestString, "split")

The error here is, "Value of type 'String' cannot be converted to a '1-dimensional array of String'." You might think it should be convertable, but it isn't.

It's almost equivalent, but you do get a slightly different error if you try executing the ToArray method against the string:

Dim TestString() As String =
    "How many sheets could a sheet splitter split".ToArray
TestString = Filter(TestString, "split")

In this case, you get the excuse, "because 'Char' is not derived from 'String'" added to the previous message.

The reason Microsoft's documentation example never has these problems is that their example starts with a String array. Do you normally declare your strings as string arrays? Neither do I.

The solution is to convert the String object into a String array. There are a number of ways that could be done, but the Split function is usually best. This code works:

Dim TestString As String =
    "How many sheets could a sheet splitter split"
Dim TestStringArray() = Split(TestString, " ")
TestStringArray = Filter(TestStringArray, "split")

This gives you a new array as shown below.

Click Here to display the illustration

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Filter Function - String Processing in VB.NET

©2014 About.com. All rights reserved.