How to read a string? 
Author Message
 How to read a string?

Hi, VB Experts:
I have a simple question.  Here is a string:  "1   2 3   4" and
I want to read it into four variables.  How do I do that?  The spaces vary from
string to string, but they only have four numbers.  Your help is
greatly appreciated.

Sincerely,

Frank



Thu, 25 May 2000 03:00:00 GMT  
 How to read a string?

Frank,

I don't know if this "1 2 3 4 "thing is just an example or
if you want to do something more with expressions.
If you fi. also want 10 from "2*5" etc then you should
look at ABC\Algorithms. There are a few polish expression-
parsers(one by William Yiu himself)which basically does the
thing. Also on my homepage you can find several sources
adressing expressionparsing from a different point of view
(recursive).

Otherwise, if you only want to read stringintegers to integers
then there are a few easy measures to take:

1) Read in a string. When you reach a separator( in your case a space
or cr), you quit inputting. The inputstring can then be translated
using: a%=val(ltrim$(rtrim$(valstring$)))

For the way you have to read one key at the time you could use
Don's source in ABC\FAQs about INKEY.

Since probably you are not allowing for long integers/doubles, a good
program must also test the stringvalue if its in the bounds( you can
see some source for that in the opcode generator on my homepage).

This way it doesn't really matter how much spaces there are between
the integers.  If you want you can also ignore cr's when the last
integer is not reached with something like this:

nrints=10
dim intarray[nrints-1]
for i=0 to nrints-1 'say you need 10 integers.
     do: i$=getkey$: loop while iswhite(i$)
         instring$="":                
         do  
        intstring$=intstring$+i$
        i$=getkey$ 'see Don's sources..
         loop until i$=' ' or i$=chr$(13) or i$=chr$(10)
intarray[i]=val(ltrim$(rtrim$(intstring$)))
next                    

'As an exercise I left the  iswhite function.

Rick
[Now you tell me where in Visual Basic you put your
basic code( I only saw windows,buttons and the like ;))))

Quote:

>Hi, VB Experts:
>I have a simple question.  Here is a string:  "1   2 3   4" and
>I want to read it into four variables.  How do I do that?  The spaces vary from
>string to string, but they only have four numbers.  Your help is
>greatly appreciated.
>Sincerely,
>Frank



Thu, 25 May 2000 03:00:00 GMT  
 How to read a string?

Quote:

> Here is a string:  "1   2 3   4" and
> I want to read it into four variables.  How do I do that?  The spaces
vary from
> string to string, but they only have four numbers.

Run a loop, reading one char at a time.
   If it's a valid Character,
        then add it to the String variable you are building.
   Otherwise,
       if it's a Space (Ascii 32) or a Null (Ascii 0)
       then your new string variable is finished being built,
       and you can store it in an array, or variable.
   Then
       Run a loop until
          you stop reading Invalid Characters Or  
          You reach Len(String$)

  If it's the end of the String,
     Exit the loop
 Otherwise
     Go back and reassign the String you just built
      and loop through again

Done!



Fri, 26 May 2000 03:00:00 GMT  
 How to read a string?

Quote:

>Hi, VB Experts:
>I have a simple question.  Here is a string:  "1   2 3   4" and
>I want to read it into four variables.  How do I do that?  The spaces vary from
>string to string, but they only have four numbers.  Your help is
>greatly appreciated.
>Sincerely,
>Frank

'PARSE.BAS
'This demo program parses four integers from a char string that may
'have excess leading, trailing and delimiting spaces. These four
'integers are placed in an array.

DEFINT A-Z
DIM num(1 TO 4) AS INTEGER

CLS
test$ = "     123   35 67    5678       "
PRINT "The test char string is '"; test$; "'"

s$ = LTRIM$(RTRIM$(test$))   'strip leading and trailing spaces
s$ = s$ + " "                'add 1 trailing space for convenience

FOR n = 1 TO 4
    p = INSTR(s$, " ")  'space location behind present first "number"
    number$ = LEFT$(s$, p - 1)
    num(n) = VAL(number$)   'nth integer from string to array variable
    s$ = LTRIM$(RIGHT$(s$, LEN(s$) - p))  'deletes first "number"
NEXT n

PRINT : PRINT "The numbers entered are:": PRINT
FOR n = 1 TO 4
    PRINT "num("; n; ") =";
    PRINT USING "#####     "; num(n);
    PRINT "num("; n; ") MOD 10 = "; num(n) MOD 10
NEXT n

PRINT
PRINT "The MOD values show these are integers NOT char strings."

END

END



Fri, 26 May 2000 03:00:00 GMT  
 How to read a string?

Try this Frank,

CLS : DIM v$(5): s$ = "1    23   45   6"

FOR i = 1 TO LEN(s$)

  m$ = MID$(s$, i, 1)

  IF m$ = " " THEN z = z + 1

  IF z = 1 THEN n = n + 1

  IF m$ <> " " THEN
     z = 0
     v$(n) = v$(n) + m$
  END IF

NEXT

FOR i = 0 TO n

  PRINT v$(i)        ' Listing the variables

NEXT

--

                                   is not at the mercy of
               *                   the one with the argument.  



Sat, 27 May 2000 03:00:00 GMT  
 How to read a string?

Remove MAPSON if replying via email

Quote:
> I have a simple question.  Here is a string:  "1   2 3   4" and
> I want to read it into four variables.  How do I do that?  The spaces vary from
> string to string, but they only have four numbers.  Your help is
> greatly appreciated.

' assumes a dimensioned Array$(n)

Sub Parse$(Text$, Separator$, Array$())
NoOfWords=0
Do Until Instr(Text$, Separator$) = 0
        Place = Instr(Text$, Separator$)
        NoOfWords = NoOfWords + 1
        Array$(NoOfWords) = Left$(Place - 1)
        Text$=Mid$(Text$,Place+1)
Loop

NoOfWords = NoOfWords+1
Array$(NoOfWords)=Text$

End Sub

Rob Davis MSc MIAP
Anstey, Leicester UK. 0976 379489



Sat, 27 May 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How can I read non-string type columns?

2. Binary read to string

3. VB6 : How to read partial string within a text file

4. REPOST: Reading a string's contents as a variable

5. Help! To read a String from a file

6. client side activeX that read a string as parameter

7. client side activeX that read a string as parameter

8. How do I read a string of text from a large text file.

9. How do I read a string or integer from a registry key?

10. Text box input not to be read as string.

11. Problem reading strings from Access7 using getchunk/appendchunk/string datatype

12. Reading a string resource string from in-process dll

 

 
Powered by phpBB® Forum Software