VB icon

ckReplace (now with invalid char stripping)

Email
Submitted on: 1/24/2015 3:59:00 AM
By: Chad M. Kovac (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: VBA MS Access
Views: 2030
 
     For use with MS Access databases mostly. - this function allows you to with strip characters from a string, replace characters in a string with other characters or strip/replace all non-alpha characters (not printable) from the string.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: ckReplace (now with invalid char stripping)
' Description:For use with MS Access databases mostly. - this function allows you to with strip characters from a string, replace characters in a string with other characters or strip/replace all non-alpha characters (not printable) from the string.
' By: Chad M. Kovac (from psc cd)
'
' Inputs:strIN is the string you wish to modify
'StripChar is the character you wish to remove/replace
'ReplaceChar is the character to use in "Stripchar"s place.
'Only strIN is required.
'
' Returns:Returns the submitted string with the modifications made as a string:
'ckReplace("This is a test"," ","") returns "Thisisatest"
'ckReplace("This is a test","i","x") returns "Thxs xs a test"
'ckReplace("Sometext%MoreText") where the % represents some non printing character (like a line feed or someting - would return "SometextMoreText"
'ckReplace("Sometext%MoreText",""," ") where the % represents some non printing character (like a line feed or someting - would return "Sometext MoreText"
'**************************************

Function ckReplace(StrIN As String, Optional StripChar As String = "", Optional ReplaceChar As String = "") As String
 Dim x As Integer
 x = 1
 If StripChar <> "" Then
Do Until x <= 0 Or StripChar = ReplaceChar
 x = InStr(1, StrIN, StripChar)
 If x > 0 Then StrIN = left$(StrIN, x - 1) & ReplaceChar & Right$(StrIN, Len(StrIN) - (x - 1) - Len(StripChar))
Loop
 Else
For x = 1 To Len(StrIN)
 If x > Len(StrIN) Then Exit For
 If Asc(Mid$(StrIN, x, 1)) < 32 Or Asc(Mid$(StrIN, x, 1)) > 126 Then
StrIN = left$(StrIN, x - 1) & ReplaceChar & Right$(StrIN, Len(StrIN) - (x - 1) - 1)
If ReplaceChar = "" Then x = x - 1
 End If
Next
 End If
 ckReplace = StrIN
End Function


Other 3 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 Advanced 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


 There are no comments on this submission.
 

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.