Re: String to Number ?

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 05/10/04


Date: Mon, 10 May 2004 11:04:23 -0400

I guess it's up to me to provide the one-liner solution? <g>

I'm guessing the space-dash-space characters, taken together, is the
actual delimiter between the comment and the numbers (and that each
record ends with the shown <br>), so... if this is a line of text you
got from your file

Record = "Aflatoxin_liver (a liver-damaging toxin produced by certain
food molds) - 344, 510.1, 943, 474, 476.6, 568<br>"

then this will store the number parts in a variant array.

Numbers = Split(Split(Replace$(Record, "<br>", ""), " - ")(1), ",")

which you can address as you would any other array. For example, here is
a listing of 100 times each number in the record string...

For X = 0 To UBound(Nums)
  Debug.Print Numbers(X)
Next

By the way, you would Dim Numbers As Variant when declaring it. As an
array of variant values, the individual values in the Numbers array are
either Strings or numbers as necessary. Note that variants are
**slower** to work with than "normal" data types, so this solution is
not necessarily considered efficient. If you have a very large number of
records to process, then this solution will be somewhat slower than the
others posted. Also Split and Replace are considered slow functions too;
although on such a small string of characters, the inefficiency here
should be minimal. The solution I posted is more of an "interesting
construction" than a general, use-it-everywhere proposal. (I've posted
it more for those regulars out there who expect me to come up with
one-liners.<g>)

Rick - MVP

"Kevala" <kevala@pacific.net.au> wrote in message
news:VkHnc.18389$KS1.321504@nasal.pacific.net.au...
> Thank you,
>
> Sorry for not quoting an entire string as a sample. Below is one of
the
> strings I am processing.
>
> Aflatoxin_liver (a liver-damaging toxin produced by certain food
molds) -
> 344, 510.1, 943, 474, 476.6, 568<br>
>
> I have been marching along the string in a loop using the Mid
function. The
> first trigger for the end of the description is the space character.
The
> trigger for end of comments is the ")" character. Then follows the
numbers.
> I just count over the three characters after the ")" character before
the
> first digit of the number begins. The trigger for the last number is
the "<"
> character in <br>
>
>
> "J French" <erewhon@nowhere.com> wrote in message
> news:409f2d26.10824226@news.btclick.com...
> > On Mon, 10 May 2004 14:40:09 +1000, "Kevala" <kevala@pacific.net.au>
> > wrote:
> >
> > >Hello,
> > >
> > >I am reading a text file in VB6 using line input.
> > >
> > >The first part of the line input string is comments that stay as
strings.
> > >
> > >The last part of the line input string is formatted as such ...
> > >
> > >"788.6, 13.29, 340.2" ... etc to the end of the line.
> > >
> > >I am currently reading each character and building strings for
> distributing
> > >to a database.
> > >
> > >My question is, is there an easy way of converting a string such as
> "788.6"
> > >to the same value and placing it into a single precision variable?
> > >
> > >Or as I have thought, just writing a temp file, dumping the string
to a
> file
> > >and then re-opening it and reading the data back in as single
precision
> > >variables.
> > >
> >
> > If you /know/ the data uses '.' for the Decimal seperator then use
Val
> > eg:
> > MySingle = Val( TheString )
> >
> > If you need it to be locale aware then:
> > MySingle = CLng( TheString )
> >
> >
>
>



Relevant Pages

  • Re: Replace space in text file
    ... In four of the above cases you have specified which type of variable you want VB to create (Integer, Long, String or whatever). ... in the case of vChars you have not specified the required variable type and VB will therefore create it as a Variant. ... For example, if vChars was an Integer then VB would read two bytes from the file, and if it was a Long it would read four bytes, and if it was a standard variable length String which currently contained nine characters then it would read nine characters from the file and if it was a String that currently contained just one character then it would read one character from the file. ...
    (microsoft.public.vb.general.discussion)
  • Re: Split Function Creates Error 13 Type Mismatch
    ... A Variant can hold a string and an array of string. ... Dim vAs Variant ... As it is, was Variant accept an array of string returned by Array, but not by Split. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Grouping shapes in Word
    ... The reason that it expects a Variant array as opposed to a String array is that ... a Variant can hold data of any type. ... Variant array of Shape names (string) or a Variant array of shape index numbers ... Dim avarShape() As Variant ...
    (microsoft.public.office.developer.vba)
  • Re: Sorting a variant array
    ... Dim tempList As Variant ... Dim testerList() As String ... Function sortTesters(rangeName As String) ... column range--but even that ends up as a x-rows by 1 column array. ...
    (microsoft.public.excel.programming)
  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... And then one to loop across the string calling that function ... copying at to a new array. ... are any characters other than lowercase letters */ ...
    (comp.lang.c)

Quantcast