Link to home
Start Free TrialLog in
Avatar of LJ083198
LJ083198

asked on

strip characters

A file is open as Random.  I need to look at a field and strip the alphas out so all I have left are numerics.  I have created a user defined type, and can get to the field this way.  I am at a loss as to how to get the program to read one character at a time and get rid of the letters.  
Avatar of cymbolic
cymbolic

see the following
A subroutine that will translate out any character and replace it with any other character string:

Public Function SubTran$(Inst$, lookfor$, ByVal Change2$)
 Dim i, j As Integer
 Dim InString$
 InString$ = Inst$
 j = 1
 i = InStr(j, InString$, lookfor$)
 While i > 0
   InString$ = Left$(InString$, i - 1) + Change2$ + Mid$(InString$, i + Len(lookfor$))
   j = i + Len(Change2$)
   i = InStr(j, InString$, lookfor$)
 Wend
 SubTran$ = InString$
End Function
fld=field
result=""
for i=1 to len(fld)
  ch=asc(mid(fld,i,1))
  if ch >= 48 and ch <= 57 then result=result & chr$(ch)
next i


Result should ony contain numerics after this routine.
A vb version of the translate instruction:

Public Trn$
'somewhere do once up front (form load? read from file?)
'set up translate table
dim i as integer
trn$=space$(256)  'default to spaces when not a number
for i=48 to 57  'Ascii 0 to Ascii 9
 mid$(trn$,i,1)=chr$(i)
next i

'then to translate
x.fld=xlt$(x.fld)

'then a subroutine
public function xlt$(instr$)
dim x$
dim i as integer
x$=space$(len(instr$))
for i =1 to len(x$)
 mid$(x$,i,1)=mid$(trn$,asc(mid$(instr$,i,1)),1)
next i
xlt$=x$
end function
or, what sbmc sez, cause he typed it in before I got around to it, but that was next, but only if you want to just concatenate all numerics together.  The key is knowing where in the Ascii range of 0 to 255 the number characters fall.  

On a vb functional basis, there is always the IsNumeric function, but that carries a load of code around to check for numeric in an entire string.
Avatar of LJ083198

ASKER

It is not that this answer is unsatisfactory, I just used the solution provided by sbmc.  It seemed much more straightforward, and it worked. As far as sbmc answer goes, I thought it was an A.
There's also some function API that you can use wich are isalpha()  and isnumeric()  if you need more info let me know about it.
ASKER CERTIFIED SOLUTION
Avatar of vijayraj
vijayraj

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