Remove all punctuation

  • Thread starter Thread starter John Thomas
  • Start date Start date
J

John Thomas

Is there a way to Remove all punctuation from a text string, without
identifying each type, if not what is the best way to do it with the text as
variable.

Thanks John
 
This would require a custom function

UNTESTED AIR CODE - Not as efficient as it could be, but it should work

Public Function fStripPunctuation(strIN)
Dim lngChar as Long
Dim lngCount as Long
Dim strReturn as String
Dim strSkipChar as string

If IsNull(StrIN) then
fStripPunctuation = Null
exit function
Else

strSkipChar = ".,!?:;" 'All punctuation characters
'Add any others that you want to include such as (,{,[
lngChar = Len(strIn & vbNullString)

For lngCount = 1 to lngChar
If Instr(1,strSkipChar, Mid(strIn,lngcount,1) ) = 0 then
strReturn = strReturn & Mid(strIn,lngCount,1)
End If
Next lngCount
End if

fStripPunctuation = strReturn

End Function
 
While I was waiting I came up with this procedure, that for the most part,
solves my problem, but I'm a novice and would like to know if it is good or
bad programimg. I like that I don't have to guess what is coming up in the
text to deal with, except for a few, and that could be a plus.

'with var106 as text variable, following loop strips all punctuation, except
for ^_[ ]\ at least as far as I could find
var111 = Len(var106)
var114 = ""
Do Until var111 = 0
If Left(var106, 1) Like "[aA-zZ]" Then
var111 = Len(var106)
var113 = Left(var106, 1)
var114 = var114 & var113
var106 = Mid(var106, 2, var111)
Else:
var111 = Len(var106)
var106 = Mid(var106, 2, var111)
End If
Loop
var106 = var114 ' ends punctuation removal


John Spencer (MVP) said:
This would require a custom function

UNTESTED AIR CODE - Not as efficient as it could be, but it should work

Public Function fStripPunctuation(strIN)
Dim lngChar as Long
Dim lngCount as Long
Dim strReturn as String
Dim strSkipChar as string

If IsNull(StrIN) then
fStripPunctuation = Null
exit function
Else

strSkipChar = ".,!?:;" 'All punctuation characters
'Add any others that you want to include such as (,{,[
lngChar = Len(strIn & vbNullString)

For lngCount = 1 to lngChar
If Instr(1,strSkipChar, Mid(strIn,lngcount,1) ) = 0 then
strReturn = strReturn & Mid(strIn,lngCount,1)
End If
Next lngCount
End if

fStripPunctuation = strReturn

End Function

John said:
Is there a way to Remove all punctuation from a text string, without
identifying each type, if not what is the best way to do it with the text as
variable.

Thanks John
 
I'd suggest that John Spencer's code is better, because it only works on the
characters that you don't want, whereas your code examines every character
to decide whether or not to keep it. Since there will normally be far more
characters you do want than you don't want, it's more efficient to only work
on the exceptions.

The other things I'd suggest is to use meaningful variable names, declare
the type of each variable using a Dim statement, and indent your code.

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



John Thomas said:
While I was waiting I came up with this procedure, that for the most part,
solves my problem, but I'm a novice and would like to know if it is good or
bad programimg. I like that I don't have to guess what is coming up in the
text to deal with, except for a few, and that could be a plus.

'with var106 as text variable, following loop strips all punctuation, except
for ^_[ ]\ at least as far as I could find
var111 = Len(var106)
var114 = ""
Do Until var111 = 0
If Left(var106, 1) Like "[aA-zZ]" Then
var111 = Len(var106)
var113 = Left(var106, 1)
var114 = var114 & var113
var106 = Mid(var106, 2, var111)
Else:
var111 = Len(var106)
var106 = Mid(var106, 2, var111)
End If
Loop
var106 = var114 ' ends punctuation removal


John Spencer (MVP) said:
This would require a custom function

UNTESTED AIR CODE - Not as efficient as it could be, but it should work

Public Function fStripPunctuation(strIN)
Dim lngChar as Long
Dim lngCount as Long
Dim strReturn as String
Dim strSkipChar as string

If IsNull(StrIN) then
fStripPunctuation = Null
exit function
Else

strSkipChar = ".,!?:;" 'All punctuation characters
'Add any others that you want to include such as (,{,[
lngChar = Len(strIn & vbNullString)

For lngCount = 1 to lngChar
If Instr(1,strSkipChar, Mid(strIn,lngcount,1) ) = 0 then
strReturn = strReturn & Mid(strIn,lngCount,1)
End If
Next lngCount
End if

fStripPunctuation = strReturn

End Function

John said:
Is there a way to Remove all punctuation from a text string, without
identifying each type, if not what is the best way to do it with the text as
variable.

Thanks John
 
Back
Top