audiophonic
audiophonic

Reputation: 171

I cant convert from string to int

Im using regex to scrape the number from website and then i want to mypltiply that by 4.

Dim thepage As String = postreqreader.ReadToEnd

Dim r As New System.Text.RegularExpressions.Regex("Views"">\(....)")
Dim matches As MatchCollection = r.Matches(thepage)

Dim kwota As String

For Each itemcode As Match In matches
    kwota = itemcode.Groups(1).Value
Next

Dim stringToInteger As Integer = Convert.ToInt32(kwota)

After that I've got an error: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll. I dont know how to make it. I'm using Microsoft Visual Studio, VB .net. The kwota variable contains 15.2 as a string.

Upvotes: 0

Views: 101

Answers (3)

Jeroen
Jeroen

Reputation: 460

Change your regex. Where you simply have 5 dots could be written as: \d+\.\d+ which means, match one or more numerics followed by a literal period followed by one or more numerics. That way you know you are grabbing a number and not whatever other crap may be on that web page.

Disclaimer: I would have to see the HTML to see if the regex is "fit for purpose" the way it is written.

Upvotes: 0

JerryM
JerryM

Reputation: 885

The Val function is fairly robust. Try:

Dim stringToInteger As Integer = CInt(Val(kwota))

Upvotes: 1

ExitusLSU
ExitusLSU

Reputation: 81

If you want to convert to integer, convert the string to a float, then to integer.

Dim stringToInteger As Integer = Convert.ToInt32(Convert.ToDouble(kwota))

Upvotes: 0

Related Questions