Link to home
Start Free TrialLog in
Avatar of mmahdi
mmahdi

asked on

Trimming unwanted characters from a string

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
ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of mark2150
mark2150

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.

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.
Avatar of mmahdi

ASKER

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
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
_______________________________________________________________