AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Don Schullian
Expertise
I`ve worked in BASIC since the Commodore days and maintain a web site part of which is devoted to helping others with thier problems. I`m proficient in all versions of PowerBASIC but cross over to QuickBASIC and Qbasic frequently.

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > Quic Basic string manipulation

Qbasic, Quickbasic - Quic Basic string manipulation


Expert: Don Schullian - 2/22/2009

Question
Hi Don,
I am working in QB 4.5 and trying to manipulate a string that represents sample information
Below is an example string
"=e804f030,28-30cm,0.2987in,s2008256,ct2008300,15.317g,c2008270—"

I would like to remove all non alpha numeric characters except decimal, dash, and comma and then put each part of the string separated by a comma into its own string for expert

the above string would be transformed into the following:

s1$=e804f030
s2$=28-30cm
s3$=0.2987in
s4$=s2008256
s5$=ct2008300
s6$=15.317g
s7$=c2008270

Once the information was divided up, would it then be possible to remove the alpha characters on some of the individual strings? For example, I'd like to keep the alpha information in the first two new strings but remove it from the rest e.g.,
s7$=c2008270 becomes 2008270

Thanks in advance.

Best regards,

Tim  

Answer
Hi Tim,

 The code below seems to work. I wrote routines like these decades ago but couldn't find the originals. So..... I think these work. The SUB ParseString will only work correctly when the Sep$ is one character in length.

 BTW, I don't have a DOS compiler loaded any more so you may have to fuss with the code a bit to get it to work in QB as I couldn't test it directly.

FUNCTION fRetain$ ( InString$, Keep$ )

 FOR P% = 1 TO LEN(InString$)
   C$ = MID$(InString$,P%,1)
   IF INSTR(Keep$,C$) > 0 THEN
     OutString$ = OutString$ + C$
   END IF
 NEXT

 fRetain$ = OutString$

END FUNCTION

FUNCTION fParseCount% ( InString$, Sep$ )

 L% = LEN(Sep$)
 P% = 1-L%

 DO
   C% = C% + 1
   P% = P% + L%
   P% = INSTR(P%,InString$, Sep$)
 LOOP UNTIL P% = 0

 fParseCount% = C%

END FUNCTION

SUB ParseString ( InString$, Sep$, Arr$() )

 Last% = UBOUND(Arr$())

 FOR C% = 1 TO Last%
   P1% = P1% + 1
   P2% = INSTR(P1%,InString$,Sep$)
   IF P2% = 0 THEN P2% = LEN(InString$) + 1
   Arr$(C%) = MID$(InString$,P1%,P2%-P1%)
   P1% = P2%
 NEXT

END SUB

N$   = ",=e804f030,28-30cm,0.2987in,s2008256,ct2008300,15.317g,c2008270—,"
K$   = "0123456789,-.abcdefghijklmnopqrstuvwxyz"
Sep$ = ","
N$   = fRetain$(N$,K$)
C%   = fParseCount%(N$,Sep$)
DIM S$(C%)
ParseString(N$,Sep$,S$())

PRINT N$
PRINT ""
WHILE C% > 0
  PRINT CHR$(34); S$(C%); CHR$(34)
  C% = C% - 1
WEND

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.