Home All Groups Group Topic Archive Search About

How to identiy numerics in a string?

Author
21 Nov 2006 8:29 PM
Learner
Hello,
   I have a situation that I need to be able to identify if there are
any numerical values in a string variable that might have numerics
including charecters.

For instanse Dim strValue as stirng

strValue  = "Inside Diameter=9"

then how to identify the numerical value 9 in it.

Thanks
-L

Author
21 Nov 2006 8:40 PM
Spam Catcher
"Learner" <pra***@gmail.com> wrote in news:1164140977.980534.137720
@h48g2000cwc.googlegroups.com:

> Hello,
>    I have a situation that I need to be able to identify if there are
> any numerical values in a string variable that might have numerics
> including charecters.
>
> For instanse Dim strValue as stirng
>
> strValue  = "Inside Diameter=9"
>
> then how to identify the numerical value 9 in it.

Use regular expressions to search for numerics [0-9](1,) (numeric number 1
char or longer)
Author
21 Nov 2006 9:11 PM
Jaap Bos
Show quote Hide quote
"Spam Catcher" <spamhoneypot@rogers.com> schreef in bericht
news:Xns98829F62B32C4usenethoneypotrogers@127.0.0.1...
> "Learner" <pra***@gmail.com> wrote in news:1164140977.980534.137720
> @h48g2000cwc.googlegroups.com:
>
>> Hello,
>>    I have a situation that I need to be able to identify if there are
>> any numerical values in a string variable that might have numerics
>> including charecters.
>>
>> For instanse Dim strValue as stirng
>>
>> strValue  = "Inside Diameter=9"
>>
>> then how to identify the numerical value 9 in it.
>
> Use regular expressions to search for numerics [0-9](1,) (numeric number 1
> char or longer)

And if you do not like to use regular expressions something like:

For i As Short = 0 To st.Length - 1

Select Case Asc(st.Substring(i, 1))

Case 48 To 57

'This is a numeric value

End Select

Next



will also work.



Groeten,

Jaap
Author
21 Nov 2006 9:28 PM
Spam Catcher
Show quote Hide quote
"Jaap Bos" <jaap.bosSPAMMERSWILLBESHOT@xs4all.invalid> wrote in
news:45636b88$0$338$e4fe514c@news.xs4all.nl:

> And if you do not like to use regular expressions something like:
>
> For i As Short = 0 To st.Length - 1
>
> Select Case Asc(st.Substring(i, 1))
>
> Case 48 To 57
>
> 'This is a numeric value
>
> End Select
>
> Next
>
>
>
> will also work.

This is pretty slow ;-)
Author
21 Nov 2006 10:26 PM
Cor Ligthert [MVP]
\
>
> This is pretty slow ;-)

I doubt it, did you test it?

Often the regex is 100 times slower than a well done loop.
How do you think that the regex does its work behind the scene.

Cor
Author
22 Nov 2006 3:13 PM
Brian Tkatch
Cor Ligthert [MVP] wrote:
> \
> >
> > This is pretty slow ;-)
>
> I doubt it, did you test it?
>
> Often the regex is 100 times slower than a well done loop.
> How do you think that the regex does its work behind the scene.


It loads Perl?

:)

B.
Author
22 Nov 2006 1:51 AM
Tom Leylan
Somebody might take this opportunity to time the various solutions.  I tend
to think the regex will be slower as well.

On the other hand... the FOR loop isn't terminating early if it finds a hit
and (the request was a bit ambiguous) but it doesn't actually identify if
there is a "9" in it.  If the OP meant by citing that example that "it has a
digit" (returning boolean) that's one thing if he wants to know where the
digit(s) are or to retrieve the entire numeric value that would be another.

I'm not going to write it (done that too many times now) but I'd suggest a
while loop that does terminate when a value has been found.  And I'd opt for
having the value it found returned (probably in string format) to
accommodate the "not found" response.  Somebody might like to consider
handling negative numbers as well.  Won't happen on an "inside diameter" of
course but it could in other situations.  And perhaps the comma and period
separators as well, as with; "Inside Diameter=8.5"

Tom


Show quoteHide quote
"Jaap Bos" <jaap.bosSPAMMERSWILLBESHOT@xs4all.invalid> wrote in message
news:45636b88$0$338$e4fe514c@news.xs4all.nl...
>
> "Spam Catcher" <spamhoneypot@rogers.com> schreef in bericht
> news:Xns98829F62B32C4usenethoneypotrogers@127.0.0.1...
>> "Learner" <pra***@gmail.com> wrote in news:1164140977.980534.137720
>> @h48g2000cwc.googlegroups.com:
>>
>>> Hello,
>>>    I have a situation that I need to be able to identify if there are
>>> any numerical values in a string variable that might have numerics
>>> including charecters.
>>>
>>> For instanse Dim strValue as stirng
>>>
>>> strValue  = "Inside Diameter=9"
>>>
>>> then how to identify the numerical value 9 in it.
>>
>> Use regular expressions to search for numerics [0-9](1,) (numeric number
>> 1
>> char or longer)
>
> And if you do not like to use regular expressions something like:
>
> For i As Short = 0 To st.Length - 1
>
> Select Case Asc(st.Substring(i, 1))
>
> Case 48 To 57
>
> 'This is a numeric value
>
> End Select
>
> Next
>
>
>
> will also work.
>
>
>
> Groeten,
>
> Jaap
>
>
>
>
>
>
Author
21 Nov 2006 9:38 PM
Learner
Hello,
  Thank you for the quick replies. I am not sure how to use regular
expressions to check for the numericals in a string. Could you please
explain with an example.

Thanks in advance.
-L
Spam Catcher wrote:
Show quoteHide quote
> "Learner" <pra***@gmail.com> wrote in news:1164140977.980534.137720
> @h48g2000cwc.googlegroups.com:
>
> > Hello,
> >    I have a situation that I need to be able to identify if there are
> > any numerical values in a string variable that might have numerics
> > including charecters.
> >
> > For instanse Dim strValue as stirng
> >
> > strValue  = "Inside Diameter=9"
> >
> > then how to identify the numerical value 9 in it.
>
> Use regular expressions to search for numerics [0-9](1,) (numeric number 1
> char or longer)
Author
21 Nov 2006 9:57 PM
Conrad Akunga [Visual C# MVP]
Here you go:

'
' Declare your regex. We want any number in a given string
'
dim r as new System.Text.RegularExpressions.Regex("(?<number>\d+)")
'
' Get your input
'
dim test as string = "Inside Diameter=9"
'
' Do the capture
'
dim result as string = r.Match(test).Groups("number").Value

On 21 Nov 2006 13:38:25 -0800, "Learner" <pra***@gmail.com> wrote:

>Hello,
>  Thank you for the quick replies. I am not sure how to use regular
>expressions to check for the numericals in a string. Could you please
>explain with an example.
>
--

Bits.Bytes.
http://bytes.thinkersroom.com