Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2912
  • Last Modified:

Remove characters from a string

Hi,

I am using the code below to remove characers from a string. It works but I would like to know if it can be done a little more "elegantly". I also would like to know how to remove double quotes ' " '.

filename = "My. ' / \ : * ?   Best, Camp( ) { } % ~ & ` ! + = $ @ aign"

'LOOK FOR THESE CHARACTERS: , . ' / \ : * ? " <> | # <TAB> ( ) { } % ~ & ` ! + = $ @
if InStr(filename, ",") then filename = Replace(filename, ",", "")
if InStr(filename, ".") then filename = Replace(filename, ".", "")
if InStr(filename, "'") then filename = Replace(filename, "'", "")
if InStr(filename, "/") then filename = Replace(filename, "/", "")
if InStr(filename, "\") then filename = Replace(filename, "\", "")
if InStr(filename, ":") then filename = Replace(filename, ":", "")
if InStr(filename, "*") then filename = Replace(filename, "*", "")
if InStr(filename, "?") then filename = Replace(filename, "?", "")
'double quotes
if InStr(filename, "<") then filename = Replace(filename, "<", "")
if InStr(filename, ">") then filename = Replace(filename, ">", "")
if InStr(filename, "|") then filename = Replace(filename, "|", "")
if InStr(filename, "#") then filename = Replace(filename, "#", "")
if InStr(filename, "(") then filename = Replace(filename, "(", "")
if InStr(filename, ")") then filename = Replace(filename, ")", "")
if InStr(filename, "{") then filename = Replace(filename, "{", "")
if InStr(filename, "}") then filename = Replace(filename, "}", "")
if InStr(filename, "%") then filename = Replace(filename, "%", "")
if InStr(filename, "~") then filename = Replace(filename, "~", "")
if InStr(filename, "&") then filename = Replace(filename, "&", "")
if InStr(filename, "`") then filename = Replace(filename, "`", "")
if InStr(filename, "!") then filename = Replace(filename, "!", "")
if InStr(filename, "+") then filename = Replace(filename, "+", "")
if InStr(filename, "=") then filename = Replace(filename, "=", "")
if InStr(filename, "$") then filename = Replace(filename, "$", "")
if InStr(filename, "@") then filename = Replace(filename, "@", "")
filename = Replace(filename, " ", "")


<%= "The output is: " & filename %>

Thanks in advace for your response.

BB
0
baxleyb
Asked:
baxleyb
  • 4
  • 3
  • 2
  • +2
2 Solutions
 
jmundsackCommented:
(Warning: Air Code)

Function RemoveUnwantedChars(ByVal Orig, ByVal Unwanted)
    Dim i
    Dim s
    s = ""
    For i = 1 To Len(Orig)
        If InStr(Unwanted, Mid(Orig, i, 1)) = 0 Then s = s & Mid(Orig, i, 1)
    Next
    RemoveUnwantedChars = s
End Function

<%

Dim filename
filename = "My. ' / \ : * ?   Best, Camp( ) { } % ~ & ` ! + = $ @ aign"
filename = RemoveUnwantedChars(filename, ",.'/\:*?""<>|#(){}%~&`!+=$@" & vbTab)
Response.Write "The output is: " & filename

%>

0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
you could use regular expressions or just create an array with all the bad charactors in it and cycle through them like:

dim myArray()
myArray = Array("!", "@", "#")   'continue adding the charactors you want out

for each element in myArray
   fileName = Replace(fileName, elemet)
next

no need to have the IF STATEMENT because if the charactor isn't found nothing will happen

to do double quotes just do:
fileName = Replace(fileName, """")

B.D.
0
 
baxleybAuthor Commented:
jmundsack,

Works perfect, thanks.

I have 1 question:

What does ByVal Orig and ByVal Unwanted mean?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
WMIFCommented:
regular expressions is gonna be your fastest.

<%
smsg = "[,.'\/\\:\*\?""<>|#\t\(\)\{\}%~&`!\+=$@]"

Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = "[,.'\/\\:\*\?""<>|#\t\(\)\{\}%~&`!\+=$@]"

response.write smsg & "<br>"
sMsg = regEx.REPLACE(sMsg,"")
response.write smsg & "<br>"
%>
0
 
jmundsackCommented:
ByVal Orig is the parameter that you pass in for the original string, ByVal Unwanted is the string of unwanted characters to remove from the original string.  I declared the parameters ByVal (passed by value, as opposed to by reference) just as a matter of habit.

0
 
baxleybAuthor Commented:
jmundsack,

I don't understand I see where Orig and unwanted are being used but I don't see where "ByVal" is being used? Can I remove ByVal and it should still work right?

Thanks

-BB
0
 
jmundsackCommented:
If you remove ByVal it will default to ByRef.  In this case there will be no effect.  I generally avoid ByRef so that I don't inadvertently cause bugs (or someone who modifies my code later).  Variables passed ByRef can be modified in the procedure and those changes will be made to the argument variable in the calling code.  It's just a convention I follow to explicitly declare parameters ByVal unless I specifically want them to be ByRef (in which case I explicitly declare them ByRef--I'm not big on defaults).
0
 
baxleybAuthor Commented:
Ok.

Is ByVal or ByRef reserved words or built funnctions or something? I don't see how they are being used anywhere else. I mean the code works but I really want to understand what those 2 are doing.

Thanks,

BB
0
 
aianrnoensCommented:
ByVal and ByRef are both directives that you use in a procedure to tell the computer whether to pass the reference to the variable to the procedure or not.

ByVal tells the computer to just pass the contents of the variable to the procedure.
ByRef tells the computer to pass the actual memory reference to the procedure.

For example

This code would not work
<script language="vbsript" runat="server">
   sub changeto1(byval var)
      var = 1
  end sub
</script>

<%
dim a
a = 3
response.write a
changeto1(a)
response.write a
%>

both response statements would output 3 to the screen


This code would work

<script language="vbscript" runat="server">
sub changeto1(byRef var)
 var = 1
end sub
</script>

<%
dim a
a = 3
response.write a
changeto1(a)
response.write a
%>

the first response statement would output 3
the second would output 1
this is because the first time you are just passing the value of the variable to the procedure.
the second time you are passing the actual memory reference to the procedure
0
 
baxleybAuthor Commented:
aianrnoens,

Thank you so much for that explanation.  It made no sense to me why it was working, but I was glad it was working.

I am increasing the point value and will give 50 to aianrnoens  and 100 to jmundsack.

Thanks,

BB
0
 
aianrnoensCommented:
Glad to help!!!  - Thanks for the points
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now