Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / November 2006

Tip: Looking for answers? Try searching our database.

How to identiy numerics in a string?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Learner - 21 Nov 2006 20:29 GMT
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
Spam Catcher - 21 Nov 2006 20:40 GMT
"Learner" <pradev@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
[quoted text clipped - 6 lines]
>
> 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)
Jaap Bos - 21 Nov 2006 21:11 GMT
> "Learner" <pradev@gmail.com> wrote in news:1164140977.980534.137720
> @h48g2000cwc.googlegroups.com:
[quoted text clipped - 12 lines]
> 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
Spam Catcher - 21 Nov 2006 21:28 GMT
> And if you do not like to use regular expressions something like:
>
[quoted text clipped - 11 lines]
>
> will also work.

This is pretty slow ;-)
Cor Ligthert [MVP] - 21 Nov 2006 22:26 GMT
\

> 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
Brian Tkatch - 22 Nov 2006 15:13 GMT
> \
> >
[quoted text clipped - 4 lines]
> 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.
Tom Leylan - 22 Nov 2006 01:51 GMT
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

>> "Learner" <pradev@gmail.com> wrote in news:1164140977.980534.137720
>> @h48g2000cwc.googlegroups.com:
[quoted text clipped - 33 lines]
>
> Jaap
Learner - 21 Nov 2006 21:38 GMT
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
> "Learner" <pradev@gmail.com> wrote in news:1164140977.980534.137720
> @h48g2000cwc.googlegroups.com:
[quoted text clipped - 12 lines]
> Use regular expressions to search for numerics [0-9](1,) (numeric number 1
> char or longer)
Conrad Akunga [Visual C# MVP] - 21 Nov 2006 21:57 GMT
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

>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.