Solved

Trimming unwanted characters from a string

Posted on 1998-08-17
6
142 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

774 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