Solved

"Remove characters from string" function?

Posted on 1998-09-01
1
130 Views
Last Modified: 2010-05-03
This is really a VBScript question, but since there is no VBScript area, I hope I can find an answer here.

Question: Does anyone have a function that would remove certain characters from a source string?

E.g.,

Function Remove(OriginalString, RemoveTheseChars)
End function

...where...

Remove("123123","2")

...would return a string containing "1313" and

Remove("No Vowels Please","aeiouy")

...would return a string containing "N Vwls Pls"

Thanks.

Again...VBScript, not VB.
0
Comment
Question by:Monroe406
1 Comment
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1432481
Use the following function :

sTmp = ReplaceString("Hello", "l", "") => Heo
sTmp = ReplaceString("Hello", "l", "p") => Heppo

Function ReplaceString(sSearch As String, sSearchFor As String, sReplaceWith As String) As String
   ' *** Searches the sSearch variable for sSearchFor
   ' *** and replaces it with sReplaceWith.

   On Error GoTo ERROR__ReplaceString

   Dim lFoundLoc        As Long   ' Location of match.
   Dim lLenRemove       As Long   ' Length of string being replaced.
   
   ' *** Set length of original text to skip.
   lLenRemove = Len(sSearchFor)    ' Set location of match.
   
   lFoundLoc = InStr(1, sSearch, sSearchFor)
   
   ' If sSearchFor isn't found in sSearch
   ' just return the original string.
   If lFoundLoc = 0 Then
      ReplaceString = sSearch
      ' If match is found, return original string up to match
      ' location, concatenate new text, and search the rest of
      ' the string recursively for additional matches.
   Else
      ReplaceString = Left(sSearch, lFoundLoc - 1) & sReplaceWith & ReplaceString(Mid(sSearch, lFoundLoc + lLenRemove), sSearchFor, sReplaceWith)
   End If

EXIT_ReplaceString:
   Exit Function

ERROR__ReplaceString:
   ' Print error to Debug window and don't interrupt query.
   Debug.Print "Error Replacing String """ & sSearchFor & """ with """ & sReplaceWith & """ in text """ & sSearch & """"
   ' If there is an error, return original string
   ' and exit the function.        ReplaceString = sSearch
   Resume EXIT_ReplaceString

End Function

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question