String Functions – Library of Routines

The String_H Library

I have begun to create a code library called String_H. This library will eventually include many functions useful in string manipulation.

While there are many pieces of source code and many functions available that do string manipulation, I have found that many of those functions perform only one task or part of a set of tasks. Also, these functions are all located in different modules, which makes it harder to remember just which file the particular string manipulation function you want resides.

At the present time, String_H includes two comprehensive functions: StripChars and SplitString . Adequate documentation for each is included for each function in the comments before the actual function definition and the code is practically littered with comments describing what certain statements are doing. Enough of the ancillary stuff, let’s get to the good stuff! What do these functions do?

Unfortunately, at the time of writing these functions, I was using Access97 and VBA, so I was unable to provide sample applications. I am confident that both these functions will work with VB4, VB5, and VB6. I am not sure about VB3. I intend to provide sample applications within a few months.

StripChars

StripChars is a function that will strip any specified character(s) from a specified string. For example, I have a large string that came out of a file that was delimited as so:

"#09/04/99##10-15-00#" (excluding quotes)

– I’m only interested in the numeric portion of this string. So I need to strip out the "/", "#", "-" and "" characters. I simply call my

function:

String_H.StripChars(By Ref StringToParse as String, ByVal UnwantedChars as String).

The string "#09/04/99##10-15-00#" is passed in by reference, so the function will perform the manipulation on the actual string. After I have stripped all of the specified characters, I am left with "090499101500". Basic error handling has been included in this function; in case of an error, the original string is saved before any manipulation occurs. Should an error occur, a message box is displayed giving detailed information regarding the error, including error source, number and description. Also, the original string will be restored and the function exits gracefully.

SplitString

SplitString is a complex, comprehensive function. It does just what its name implies. It splits strings according to 1 of 8 split options that you choose and returns the portions you want in a collection. The options are the ability to split a string on any one character or individual characters contained in the string you want to split. You can also choose to split a string on any one character or a string of individual characters and retain those characters. Finally, you can also split a string on any one character or a string of individual characters and keep those characters with either the characters preceding the split condition or following the split condition up to but not including the next split condition. Here’s an example of each:

Here are the strings we will be using for each example:

StringToSplit = "/Hello_/ World. /_ How/_ _/are_/_ you?__/"

StringToSplitOn = "/_"

StringToSplitOn here represents the characters on which we want to split StringToSplit according to the split option chosen.

Split on a character or individual character:

In this example, we’re splitting the string on the individual characters (if you choose, you could do only one of the characters) and discarding any instance of "/" and "_" found in StringToSplit. The result for this example would be:

"Hello", " World. ", " How", " ", "are", and " you?".

Each of the strings would be in a separate item in the collection. The only thing you may need to do is reconcatenate them into something meaningful to you.


Split on a character and include the split character(s) as a separate entry in the collection:

This option allows you to retain the split characters found in StringToSplit as separate items in the collection. The result would be as follows:

"/", "Hello", "_", "/" "World. ", "/", "_", "How", "/", "_", " ", "_", "/", "are", "_", "/", "_", " you?", "_", "_", "/"

Split on a character and include the split character(s) with any non-split characters preceding it:

This option allows you to retain the split characters and store them with any non-split characters found preceding it in the collection. The result would be as follows:

"/", "Hello_", "/", "World. /", "_", "How/", "_", "_", "/", "are_", "/", "_", " you?_", "_", "/"


Split on a character and include the split character(s) with any non-split characters following it :

This option is similar to the previous, but instead stores split characters with all non-split characters following it. Here’s the result:

"/Hello", "_", "/ World. ", "/", "_", "_ How", "/", "_ ", "_", "/are", "_", "/", "_ you?", "_", "_", "/"

Now the above looks pretty confusing because we’re splitting StringToSplit on the instance of one or more individual characters. But the same options are available to split on a string (which of course could also be a singular character and would therefore act the same as the split on character option). Here are examples of those options:

Here are the strings we will be using for each example:

StringToSplit = "/Hello_/ World. /_ How/_ _/are_/_ you?__/"

StringToSplitOn = "/_"

StringToSplitOn here represents the characters on which we want to split

StringToSplit according to the split option chosen.

Split StringToSplit on any occurrence of StringToSplitOn:

This option will split StringToSplit where ever a "/_" occurs and discard any "/_" found. Here are the results:

"/Hello_/ World. ", " How", " _/are_", " you?__/"

To use a simpler example: "Hello_World" and "_" returns "Hello" and "World".


Split StringToSplit on any occurrence of "/_" and store "/_" as a separate item in the collection:

Again, similar to the second option available with splitting characters. The result would be as follows:

"/Hello_/ World. ", "/_", " How", "/_", " _/are_", "/_", " you?__/".

Again, a simpler example: "Hello_World" and "_" returns "Hello", "_" and "World".


Split StringToSplit on any occurrence of "/_" and store "/_" with any non-split strings preceding it:

This options is similar to the thrid one described for splitting on characters. The results would be as follows:

"/Hello_/ World. /_", " How/_", " _/are_/_", " you?__/"

A simpler example: "/_Hello/_World/_" and "/_" returns:

"/_", "Hello/_", and "World/_".


And finally, Split StringToSplit on any occurrence of "/_" and store "/_" with any non-split strings following it :

Similar to option four under character options above. Here are the results:

"/Hello_/ World. ", "/_ How", "/_ _/are_", "/_ you?__/"

A simpler example: "/_Hello/_World/_" and "/_" returns:

"/_Hello", "/_World", "/_"


All I can say is it’s a complex function. You need to play around with it in order to truly see how it works. Again, I hope to provide sample applications soon.

I’m really looking to get feedback on these functions. If you find a bug (which I don’t think there are any, I ran these things through the ground to make sure), or if you have improvements or enhancements that you would like to see, please let me know.

Future enchancements include the following:

  • provide an option in SplitString to capitalize the string you want to split so that your split criteria can be case insensitive
  • provide an option in SplitString so that if two consecutive characters in the string you want to split are the same split character, store those characters together as one item in the collection.

Again, your feedback is important!!

All source code has been formatted so that it can be viewed horizontally in the VB Editor window using its default font settings. Also, it has been formatted so that all code is indented and all lines will print on only one lines (i.e. lines won’t wrap!!).

Download Zipped String_H.Bas (10k)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read