473,424 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 software developers and data experts.

Seeking suitable A97 FN for removing punctuation from a string

MLH
Back in mid-2003, lucason posted a question about removing
punctuation chars from a string. Suggested code was posted
using Replace function. Could the FN below be easily modified
for use with A97 which has no replace FN?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxx
lucason
View profile
More options Aug 20 2003, 5:52 am
Newsgroups: comp.databases.ms-access
From: luca...@hotmail.com (lucason)
Date: 20 Aug 2003 02:52:36 -0700
Local: Wed, Aug 20 2003 5:52 am
Subject: Re: Remove Specific Characters From A text box
Reply to author | Forward | Print | Individual message | Show original
| Report this message | Find messages by this author

csgraha...@hotmail.com (Colin Graham) wrote in message
<news:ee**************************@posting.google. com>...
I need to write a function to remove any sort of punctuation from a
text box and only allow the characters from A to Z. E.g. If i type in
"Róisín" then i want the system not to allow this, even better if it
could change it to "Roisin" or if possible to remove any character
which isnt between A to Z. i am a bit stuck with this. Can anyone
please help????
You didn't look very far before asking the question did you....

Use the replace command!

Put it in a function if you like:

'StartCode-----------------------
Public Function FixString(strStringToChange As String) As String
Dim x As Integer

For x = 1 To Asc("A")
strStringToChange = Replace(strStringToChange, Chr(x), "")
Next x
For x = Asc("z") To 255
strStringToChange = Replace(strStringToChange, Chr(x), "")
Next x
FixString = strStringToChange
End Function
'-------------------------EndCode

If you like, you can add specific replacements:

'StartCode2-----------------------
Public Function FixString(strStringToChange As String) As String

strStringToChange = replace(strStringToChange,"ó","o")
strStringToChange = replace(strStringToChange,"í","i")
'Add as many as you like

'Then delete all remaing
Dim x As Integer

For x = 1 To Asc("A")
strStringToChange = Replace(strStringToChange, Chr(x), "")
Next x
For x = Asc("z") To 255
strStringToChange = Replace(strStringToChange, Chr(x), "")
Next x
FixString = strStringToChange
End Function
'-------------EndCode2

Plug that in and then try

? FixString ("Róis1ín")

in the debug window
Sep 2 '07 #1
3 3433
MLH
The obvious course of action would be to roll your own Replace
function like the one below. But calling the homegrown procedure
over and over again seems intense. I'm wondering if there's something
simpler...
Function ReplaceThisCharWThatChar(OriginalString As String, ThisChar
As String, ThatChar As String) As String

On Error GoTo Err_ReplaceThisCharWThatChar
Dim NewString As String, CurrentCharacter As String
Dim StringLen As Integer, CurrentCharacterPosition As Integer
OriginalString = Trim(OriginalString)
StringLen = Len(OriginalString)

If StringLen = 0 Then ReplaceThisCharWThatChar = "": Exit Function
If Len(ThisChar) <1 Or Len(ThatChar) <1 Then
ReplaceThisCharWThatChar = "": Exit Function

For CurrentCharacterPosition = 1 To StringLen
CurrentCharacter = Mid$(OriginalString, CurrentCharacterPosition,
1) 'Get the CurrentCharacter
If CurrentCharacter = ThisChar Then CurrentCharacter = ThatChar
'If the current character is a ThisChar, change it to a ThatChar.
NewString = NewString & CurrentCharacter
'Append the current char to the new string
Next CurrentCharacterPosition
ReplaceThisCharWThatChar = NewString

Exit_ReplaceThisCharWThatChar:
Exit Function

Err_ReplaceThisCharWThatChar:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in StringFunctions'
global FN ReplaceThisCharWThatChar."
k = CRLF & CRLF & str$(Err) & ": " & Quote & Error$ & Quote
Message3 = r & k
MsgBox Message3, 48, "Unexpected Error - " & MyApp$ & ", rev. " &
MY_VERSION$
Resume Exit_ReplaceThisCharWThatChar

End Function

Sep 2 '07 #2
On Sun, 02 Sep 2007 11:33:13 -0400, MLH <CR**@NorthState.netwrote:
>The obvious course of action would be to roll your own Replace
function like the one below. But calling the homegrown procedure
over and over again seems intense. I'm wondering if there's something
simpler...
Function ReplaceThisCharWThatChar(OriginalString As String, ThisChar
As String, ThatChar As String) As String

On Error GoTo Err_ReplaceThisCharWThatChar
Dim NewString As String, CurrentCharacter As String
Dim StringLen As Integer, CurrentCharacterPosition As Integer
OriginalString = Trim(OriginalString)
StringLen = Len(OriginalString)

If StringLen = 0 Then ReplaceThisCharWThatChar = "": Exit Function
If Len(ThisChar) <1 Or Len(ThatChar) <1 Then
ReplaceThisCharWThatChar = "": Exit Function

For CurrentCharacterPosition = 1 To StringLen
CurrentCharacter = Mid$(OriginalString, CurrentCharacterPosition,
1) 'Get the CurrentCharacter
If CurrentCharacter = ThisChar Then CurrentCharacter = ThatChar
'If the current character is a ThisChar, change it to a ThatChar.
NewString = NewString & CurrentCharacter
'Append the current char to the new string
Next CurrentCharacterPosition
ReplaceThisCharWThatChar = NewString

Exit_ReplaceThisCharWThatChar:
Exit Function

Err_ReplaceThisCharWThatChar:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in StringFunctions'
global FN ReplaceThisCharWThatChar."
k = CRLF & CRLF & str$(Err) & ": " & Quote & Error$ & Quote
Message3 = r & k
MsgBox Message3, 48, "Unexpected Error - " & MyApp$ & ", rev. " &
MY_VERSION$
Resume Exit_ReplaceThisCharWThatChar

End Function

You are correct. That previously offered solution was quite
inefficient. Scan the string that gets passed to the function, one
character at a time. If the ASC of the character is in range, for
example:

a = ASC(C)
if (a 64 and a < 91) or (a 96 and a < 123) then
keep the character
else
discard it

would be much more efficient that blasting through replace of every
unwanted character.
Sep 2 '07 #3
On Sep 2, 11:33 am, MLH <C...@NorthState.netwrote:
The obvious course of action would be to roll your own Replace
function like the one below. But calling the homegrown procedure
over and over again seems intense. I'm wondering if there's something
simpler...

Function ReplaceThisCharWThatChar(OriginalString As String, ThisChar
As String, ThatChar As String) As String

On Error GoTo Err_ReplaceThisCharWThatChar
Dim NewString As String, CurrentCharacter As String
Dim StringLen As Integer, CurrentCharacterPosition As Integer
OriginalString = Trim(OriginalString)
StringLen = Len(OriginalString)

If StringLen = 0 Then ReplaceThisCharWThatChar = "": Exit Function
If Len(ThisChar) <1 Or Len(ThatChar) <1 Then
ReplaceThisCharWThatChar = "": Exit Function

For CurrentCharacterPosition = 1 To StringLen
CurrentCharacter = Mid$(OriginalString, CurrentCharacterPosition,
1) 'Get the CurrentCharacter
If CurrentCharacter = ThisChar Then CurrentCharacter = ThatChar
'If the current character is a ThisChar, change it to a ThatChar.
NewString = NewString & CurrentCharacter
'Append the current char to the new string
Next CurrentCharacterPosition
ReplaceThisCharWThatChar = NewString

Exit_ReplaceThisCharWThatChar:
Exit Function

Err_ReplaceThisCharWThatChar:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in StringFunctions'
global FN ReplaceThisCharWThatChar."
k = CRLF & CRLF & str$(Err) & ": " & Quote & Error$ & Quote
Message3 = r & k
MsgBox Message3, 48, "Unexpected Error - " & MyApp$ & ", rev. " &
MY_VERSION$
Resume Exit_ReplaceThisCharWThatChar

End Function
I don't know if this will run in Access97 or not. It will ldepend on
the version of Windows being used. If it will, it should be very fast.
If it won't, it will be even faster.

Public Sub RemoveCharacters( _
ByRef InputString$, _
ByVal Characters$)
' requires that Visual Basic Script be installed
' VBS is installed by default with Windows
' it disappears only with direct action by an administrator
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
InputString = Trim(InputString)
With re
.Global = True
.IgnoreCase = True
.Pattern = "[" & Characters & "]"
InputString = .Replace(InputString, "")
End With
Set re = Nothing
End Sub

Public Sub test()
Dim test$
test = "Whatever you say, Big Fellow!"
RemoveCharacters test, "\!\,\;\:"
Debug.Print test
'Whatever you say Big Fellow
End Sub

The test removes the exclamation point, (\!), the comma (\,), the semi-
colon (\;) and the colon (\:).

You could copy the code to a 97 module, run the test sub and tell us
if it works.

Sep 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
3
by: CDMAPoster | last post by:
A.K.A. Is Double Dating a bad thing :-)? My post from several hours ago may have gotten lost so please forgive me if something similar to this shows up twice. From a modular programming class I...
2
by: Anat | last post by:
Hi, I need a little help on performing string manipulation: I want to take a given string, and make certain words hyperlinks. For example: "Hello world, this is a wonderful day!" I'd like the...
15
by: Kay Schluehr | last post by:
I have a list of strings ls = and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in . There might be...
1
by: Max | last post by:
I'm having a major issue in which I am reading data from a telnet server with socket_read, and the data apears to be coming back with what seems like either line feeds that stay on the same line or...
1
by: nickyeng | last post by:
I have done the following code, i just couldn't figure out why it can erase the punctuation in the end of each word(strr variable) ? i got this when i run it:...
3
by: Jia Lu | last post by:
Hello all I see there are lots of flat db or db-like modules in the standard python modules. What about the keywords seeking speed of them ? (I want to put about 10000 articles with 10000...
4
by: jerger | last post by:
i have a great program now with the help of a member from this site, but i need a little customization to meet the needs of non-english speakers... who might accidendtly type punctuation which would...
13
by: jerger | last post by:
my program takes users input (words/sentance) and translates it from english to hmong. I have to main variables, but cannot post my entire code. char In; CString in; basically the user cin...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.