Important alert: (current site time 7/15/2013 11:25:43 PM EDT)
 

VB icon

Beginner's Approach to String Cleanup

Email
Submitted on: 10/3/2007 7:35:51 AM
By: Eric Brophy  
Level: Beginner
User Rating: Unrated
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 5610
(About the author)
 
     Take an ordinary, dirty string and return a clean, shiny string without any symbols that your SQL server, script or whatever might choke on!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Beginner's Approach to String Cleanup
' Description:Take an ordinary, dirty string and return a clean, shiny string without any symbols that your SQL server, script or whatever might choke on!
' By: Eric Brophy
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=9580&lngWId=4'for details.'**************************************

function cleanUp(sDirty)
Dim i,n 'iterative variables
Dim sCur,sClean,iLen
Dim sOrig(),sLower(),sUpper() 'arrays to hold the broken-down string in its various states
'MODIFY THIS STRING TO ALLOW SPECIFIC SYMBOLS TO BE USED
Const sAllow = "`~!@#$^*()-_=+[]{}|\: " 
'MODIFY THIS STRING TO ALLOW SPECIFIC SYMBOLS TO BE USED
if trim(sDirty) = "" then
'if the string is empty or space-padded, don't bother with it
 cleanUp = ""
 exit function
end if
iLen = len(sDirty) 
'determine length of string
Redim sOrig(iLen): Redim sLower(iLen): Redim sUpper(iLen) 
'make the arrays the correct size (0th element will be included but not used)
for i = 1 to iLen
'populate the arrays
 sCur = mid(sDirty,i,1)
 'grab one character from position i in sDirty
 sOrig(i) = sCur
 sLower(i) = lcase(sCur)
 sUpper(i) = ucase(sCur)
next
for n = 1 to iLen
 if sLower(n) = sUpper(n) then 
 'if there was no change in the character by changing their case, which is to say that it's not a letter
 if instr(1,sAllow,sOrig(n)) then 
 'if the character exists in the sAllow const defined above ...
sClean = sClean & sOrig(n) 
 elseif isNumeric(sOrig(n)) then
 '... or if the character is numeric ...
sClean = sClean & sOrig(n)
'...then we'll still add it to the finished product.
 end if
 'if neither of those conditions are met, it will simply ignore that character and move on
 else
 'if the character did change when changing case, which means that it is a letter ...
 sClean = sClean & sOrig(n)
 end if
next
cleanUp = sClean
'return the cleaned-up string
end function


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/10/2008 3:26:28 PMDave Vroman

Interesting Idea
Some Of The Characters Allowed Are Used For Hacking Web Sites.
i.e ` and -
(If this comment was disrespectful, please report it.)

 
1/11/2008 6:14:09 PMEric Brophy

Thanks for the comment Dave!

What I was trying to achieve with this piece of code was this quite routine task (removing symbols from a string) and making sure that even a novice user could modify it to make it their own, choose which symbols would be OK. I wasn't at all aware that ` and - could be used for malicious purposes (thank you for informing me). Since this function was designed to be easy to modify though, I'm not sure that I'll update the code to remove those characters - I'm sure that you've already found for yourself that it's quite easy to do!

Another way to make this function more flexible would be to add the sAllow as a parameter rather than a constant, but I'm not sure this is necessary - and I've always been a fan of one-parameter functions!

Thanks again for the comment! :)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.