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)



Comments

  • String

    Posted by Legacy on 01/03/2004 12:00am

    Originally posted by: Matt

    Thank you.

    Reply
  • String library -- Good and thanks

    Posted by Legacy on 06/07/2003 12:00am

    Originally posted by: Bhushan Joshi

    Its a very useful library

    Reply
  • Thanks!!!!

    Posted by Legacy on 05/06/2003 12:00am

    Originally posted by: Garibaldi

    All I have to say is thank You very much!!!!!

    Reply
  • thanks a bunch !!!

    Posted by Legacy on 06/16/2002 12:00am

    Originally posted by: julian

    just wanted to thanks you for the great work u did..
    u sure as save me a lot of time !!!

    thanks,
    Julian.

    Reply
  • Excellent : better than best

    Posted by Legacy on 06/07/2002 12:00am

    Originally posted by: rao

    Excellent vb routines for programmers.Works like cham. Thanks a lot.

    Thanks again
    Rao

    Reply
  • Split strings

    Posted by Legacy on 04/30/2002 12:00am

    Originally posted by: mukund rao

    Exceptional and brilliant in concept .very useful for beginners like me. Thans a lot

    Reply
  • Great Stuff

    Posted by Legacy on 04/23/2001 12:00am

    Originally posted by: Hal Totten

    I've been looking for something like this. It's nice to see people still come out with stuff for everyone to use.

    Reply
  • double spacing

    Posted by Legacy on 12/08/2000 12:00am

    Originally posted by: Nimal

    Do you have any sample code for double spacing a rich text box.

    regards

    nimal.

    Reply
  • Thanks for your hard work

    Posted by Legacy on 11/20/2000 12:00am

    Originally posted by: Ryan White

    You have saved me a lot of time. I appreciate your efforts.

    Regards,
    Ryan D. White
    ryan@dvelco.com

    Reply
  • Thanks a lot

    Posted by Legacy on 10/31/2000 12:00am

    Originally posted by: Sudesh Nelson

    Hi Craig!

    Thanks a million for your neatly written functions. I appriciate your
    ingenuiety and the eloborate description that you have given to elicudate
    ever line of code. I have decided to go through the other code you have
    written. It is ample reliev to know that they are people like you trying to
    help us out who are learning the ropes of codeing.

    Thanks once again.

    I would feel awful if I had not thanked you for your work and contribution.

    Bye.

    Sudesh Nelson


    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds