?
Solved

VBA - Convert all Special Characters to Text

Posted on 2003-04-01
6
Medium Priority
?
2,129 Views
Last Modified: 2006-11-17
Hello,

There seems to be many ways of doing this - but I'm after a smart function that will forfill all the following string manipulation.

Background : I have a table of Word Documents that are going to sucked out, using stream read - the content becomes a String, which will be indexed by SQL Server indexing.

I have successful got the 'string' but when inserted into the 'pure text' column, contains lots of special characts (appear as square) and such like. Don't really want a clumsy 'replace(str,vbxxx," ")' over and over.

I would like

1. Anything more that a double space to be a single
2. No tabs, lf, crlf or any form of formatting like that
3. Character 1-9, A-Z + "*",".","£" etc ... to be ALLOWED (as people will search on .Net, C++ etc)

So basically, any 'normal' ascii should be fine - but ALL those strange formatting characters to be replaced by single space.

A nice callable function is what I'm after ... myString = CleanMyString(myString)

Thanks y'awl !

S.S.
0
Comment
Question by:SpencerSteel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 7

Expert Comment

by:manthanein
ID: 8244592
Function CleanMyString(strwhat As String) As String

Dim icount As Integer
Dim iFirst As Integer
CleanMyString = ""
iFirst = 0
For icount = 1 To Len(strwhat)
    If Asc(Mid(strwhat, icount, 1)) > 32 And Asc(Mid(strwhat, icount, 1)) < 127 Then
        CleanMyString = CleanMyString & Mid(strwhat, icount, 1)
        iFirst = 0
    ElseIf Asc(Mid(strwhat, icount, 1)) = 32 Then
        iFirst = iFirst + 1
        If iFirst < 3 Then
            CleanMyString = CleanMyString & Mid(strwhat, icount, 1)
        End If
    End If
Next
CleanMyString = Replace(CleanMyString, "  ", " ")
End Function
0
 

Author Comment

by:SpencerSteel
ID: 8244798
this looks great ... i say looks as my front-end *&*($ing database app has just got irreverseabley corrupt and guess who doesn't have a backup from the last day. (it opens - but no objects)

Gimme some time to get back to where I was and I'll close this question asap.

Thanks

S.S.
0
 
LVL 5

Accepted Solution

by:
burtdav earned 500 total points
ID: 8244841
If you Add Reference to something like Microsoft VBScript Regular Expressions, you can implement CleanMyString with just a few calls to RegExp.Replace:

set re = new RexExp
' rule 2 - delete tabs, newlines, carriage returns, form feeds
re.Pattern="[\t\r\n\l]"
myString=re.Replace(myString, "")
' rule 1 - adjacent whitespace becomes a single space
re.Pattern="\s+"
myString=re.Replace(myString, " ")
' rule 3 - delete everything except spaces, numbers, word chars (a-z, A-Z), *s, .s, #s
re.Pattern="[^ \d\w\*\.\#]"
myString=re.Replace(myString, "")
CleanMyString=myString
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SpencerSteel
ID: 8246115
Burtdav - don't understand that at all really (sorry!)

The other solution is working great, except one minor flaw - here's an example of the output

...Curriculum Vitae ProfileAn IT professional with over nine years functional...

In the original doc, the CV Profile would of been a heading, so you see we kind need an extra space of those CTRLs and I'm being REEEEEEALY lazy here - but i'm up to my eyeballs ... finish it off and the points are yours

Many thanks

S.S.
0
 

Author Comment

by:SpencerSteel
ID: 8252590
Doh...all I needed was

   CleanMyString = Replace(CleanMyString, Chr(13), " ")

at the end, just before

   CleanMyString = Replace(CleanMyString, "  ", " ")

Thanks

Great routine

Points awarded !

Take care

S.S.
0
 

Author Comment

by:SpencerSteel
ID: 8252591
Just what I wanted - quickly !

Thanks

S.S.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Six Sigma Control Plans
Introduction to Processes
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question