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 by any other name?

    Posted by ahoodin on 08/05/2014 05:17am

    How is this better?

    Reply
  • 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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds