Solved

Trimming unwanted characters from a string

Posted on 1998-08-17
6
139 Views
Last Modified: 2010-04-30
Hi,

I am trying to remove non alpha-numeric characters from a string extracted from a Word 97 document in my VB project.

I used OLE to read a Word file then extract let's say sentence number 3 from the open document.

The text box that displays the content of that sentence shows a little square as the rightmost character.

Is there a function I can use to trim that little square from the string??

Regards
MMAHDI
0
Comment
Question by:mmahdi
6 Comments
 
LVL 14

Accepted Solution

by:
waty earned 50 total points
Comment Utility
Here is a function to replace strings or characters by others :

ex :
   sTmp = ReplaceString(sLine, Chr$(9), Chr$(32))
   sTmp = ReplaceString(sLine, Chr$(9), "")

Code :
Public Function ReplaceString(sStart As String, sFind As String, sReplace As String) As String

   Dim nOldPos    As Long
   Dim nPos       As Long
   Dim sDest      As String

   sDest = ""
   nPos = 1
   nOldPos = 1

   nPos = InStr(nPos, sStart, sFind)

   Do While nPos > 0
      sDest = sDest & Mid(sStart, nOldPos, nPos - nOldPos) & sReplace
     
      nOldPos = nPos + 1
      nPos = InStr(nOldPos, sStart, sFind)
   Loop
   
   ' *** Copy untill the end
   sDest = sDest & Mid(sStart, nOldPos, Len(sStart) - nOldPos)
   
   ReplaceString = sDest

End Function

0
 
LVL 1

Expert Comment

by:Sekans
Comment Utility
This function will test to see if a character in a string is a valid alpha/numeric.  If it's not, it removes it.

Public Function RepStr(strTotest As String) As String
Dim iCnt As Integer
strText = strTotest
For iCnt = 1 To Len(strText)
    If iCnt > Len(strText) Then Exit For
    If Asc(Mid(strText, iCnt, 1)) < 32 Or Asc(Mid(strText, iCnt, 1)) > 126 Then
        strText = Left(strText, iCnt - 1) & Right(strText, Len(strText) - iCnt)
    End If
    DoEvents
Next iCnt
RepStr = strText
End Function

Let me know if I can help.

Regards,
Sekans

0
 
LVL 12

Expert Comment

by:mark2150
Comment Utility
How about:

Public Function RepStr(Text as String) as String
dim filter as string
dim work as string
dim char as string
dim index as integer
work = ""
filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for index = 1 to len(text)
    char = mid(text, index, 1)
    if instr(char, filter) then work = work & char
next index
'
RepStr = work
End Function

This method has the advantage of allowing explicit control over that gets accepted. You can add selected punctuation and other symbols as well as the standard character set.

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:lwariar
Comment Utility
Hi,
We had a similar problem and this seemed to work.
This function replaces the line feed character with a carrige return line feed character.
Hope this helps you.

Public Function Replace(sString As String) As String
  Dim Maxlength As Integer
  Dim CurrentPos As Integer
  Dim sNewString As String
       
  Maxlength = Len(sString)
  CurrentPos = InStr(1, sString, Chr(10), 1)
 
  While (CurrentPos > 0)
    NewString = Mid(sString, 1, CurrentPos - 1)
    sNewString = sNewString & Chr(13) & Chr(10)
    sNewString = sNewString & Mid(sString, CurrentPos + 1, Maxlength - CurrentPos)
    CurrentPos = InStr(CurrentPos + 2, sNewString, Chr(10), 1)
    sString = sNewString
    Maxlength = Len(sNewString)
  Wend

  Replace = sString
End Function

regards,
lwariar.
0
 
LVL 1

Author Comment

by:mmahdi
Comment Utility
Waty,

Your second function works.
It leaves out one non-alpha-numeric (on some sentences there was up to 4) so I used something like:

if asc(right(sentence,1))=13 then sentence=left(sentence,len(sentence)-1) it it seems to work beautifly.

Excelent help thank you!

Regards
MMAHDI
0
 

Expert Comment

by:seandean
Comment Utility
Here are 2 methods that should work........


1.)
_______________________________________________________________
Public Function StripNonAlphaNumer(strToTest As String)

strText = strToTest
strText = Replace((strText), vbCrLf, "")
strText = Replace((strText), Chr(32), "")
strText = Replace((strText), Chr(33), "")
strText = Replace((strText), Chr(34), "")
strText = Replace((strText), Chr(35), "")
strText = Replace((strText), Chr(36), "")
strText = Replace((strText), Chr(37), "")
strText = Replace((strText), Chr(38), "")
strText = Replace((strText), Chr(39), "")
strText = Replace((strText), Chr(40), "")
strText = Replace((strText), Chr(41), "")
strText = Replace((strText), Chr(42), "")
strText = Replace((strText), Chr(43), "")
strText = Replace((strText), Chr(44), "")
strText = Replace((strText), Chr(45), "")
strText = Replace((strText), Chr(46), "")
strText = Replace((strText), Chr(47), "")
strText = Replace((strText), Chr(58), "")
strText = Replace((strText), Chr(59), "")
strText = Replace((strText), Chr(60), "")
strText = Replace((strText), Chr(61), "")
strText = Replace((strText), Chr(62), "")
strText = Replace((strText), Chr(63), "")
strText = Replace((strText), Chr(64), "")
strText = Replace((strText), Chr(91), "")
strText = Replace((strText), Chr(92), "")
strText = Replace((strText), Chr(93), "")
strText = Replace((strText), Chr(94), "")
strText = Replace((strText), Chr(95), "")
strText = Replace((strText), Chr(96), "")
strText = Replace((strText), Chr(123), "")
strText = Replace((strText), Chr(124), "")
strText = Replace((strText), Chr(125), "")
strText = Replace((strText), Chr(126), "")



StripNonAlphaNumer = strText
End Function
_______________________________________________________________

2.)
_______________________________________________________________
Public Function BuildAlphaNumer(strToTest As String)
Dim POS As Integer
strText = strToTest
For POS = 1 To Len(strText)
chtocheck = Mid((strText), POS, 1)
chtocheck = "Asc(" & Chr(34) & chtocheck & Chr(34) & ")"
chtocheck = Eval(chtocheck)
If chtocheck = 48 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 49 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 50 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 51 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 52 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 53 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 54 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 55 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 56 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 57 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 65 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 66 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 67 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 68 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 69 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 70 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 71 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 72 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 73 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 74 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 75 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 76 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 77 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 78 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 79 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 80 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 81 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 82 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 83 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 84 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 85 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 86 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 87 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 88 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 89 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 90 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 97 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 98 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 99 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 100 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 101 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 102 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 103 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 104 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 105 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 106 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 107 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 108 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 109 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 110 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 111 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 112 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 113 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 114 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 115 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 116 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 117 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 118 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 119 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 120 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 121 Then buildstring = buildstring & Chr((chtocheck))
If chtocheck = 122 Then buildstring = buildstring & Chr((chtocheck))


Next

BuildAlphaNumer = buildstring
End Function
_______________________________________________________________
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now