Solved

"Remove characters from string" function?

Posted on 1998-09-01
1
127 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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now