Solved

Trimming unwanted characters from a string

Posted on 1998-08-17
6
145 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
[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
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

737 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