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
Solved

Trimming unwanted characters from a string

Posted on 1998-08-17
6
143 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
ID: 1429227
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
ID: 1429228
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
ID: 1429229
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:lwariar
ID: 1429230
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
ID: 1429231
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
ID: 14029955
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

790 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