Removing Characters and Strings from a String (VB6)

I am often faced with a problem of a string that needs to have certain characters removed from it. With the advent of the <b>Replace() </b> function, the problem becomes more manageable. For instance, if you wanted to remove all of a's from a particular string, you could do the following:

Debug.print Replace("abababa", "a", "")

This is nice when you only have a single character that you want removed, but if you have a long list of suspects, you will have to do some serious copy and paste. You can avoid that by using the following function:

public Function StripOut(From as string, What as string) as string

    Dim i as Integer

    StripOut = From
    for i = 1 to len(What)
        StripOut = Replace(StripOut, mid$(What, i, 1), "")
    next i

End Function

Just place it somewhere in your code (preferably in a module), and call it like this:

Debug.print StripOut("abcdefg", "bdf")

This will return a string that had all of its 'b', 'd', and 'f' characters removed.



Comments

  • Replace Characters in VB String

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

    Originally posted by: Partha

    This is the simplest and best that I could found on the net when I searched for morethan an hour to figure out escaping the special characters in VB.
    This code straight away provided me the clue that I was looking for and saved my time a lot.
    Thanks a lot to the writer of this code.

    Reply
  • http://codeguru.developer.com/vb/articles/1761.shtml

    Posted by Legacy on 02/27/2000 12:00am

    Originally posted by: Sebastijan

    The problem is how to stripout a word "cde" from "abcdefgccdecc cc cde cc"

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

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds