Get integer from string characters

tosto

New member
Joined
Aug 31, 2006
Messages
3
Programming Experience
Beginner
Hi everydody,

I would like to get an integer value from a string, so for example if the user type: 22 days, I get 22.


If there any way to get this value from that string

Thanks a lot
 
There are several ways to do this the easiest to understand is probably :

VB.NET:
  IntegerVar=CInt(StringVar)

A useful function when implementing this type of functionality is the IsNumeric() call. It returns true if it can convert the string to a number and is useful for testing the data for example:

VB.NET:
If  IsNumeric(StringVar) then
    IntegerVar=CInt(StringVar)
Else
    'deal with non numeric input here
End if

With this sort of code it is important to error trap it well and test it with non-numeric strings.

If the string contains text as well as a number you will need to strip that out first. The best approach would be to stop the user entering this information in the first place rather than trying to interpret a complex string. "22 Days" would be easy to process but how about: "22 or 23 days" or "it took 22 days" - it's best to deal with this at the input stage.

Use the IsNumeric() function to test for numeric input and make them do it again if it is false. To be fair to your users your form should be clear about what data it is expecting to have input and error messages should be clear.
 
You can limit user input in several ways, for example with typing validation or using numeric input controls like the NumericUpDown or Trackbar.

If the string provided is mixed alphanumerical there is much usefulness in Regular Expressions. One example:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] input [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"add 22 + 23 days"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rxq [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"\d+"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] regex [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Text.RegularExpressions.Regex(rxq)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] regmats [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Text.RegularExpressions.MatchCollection = regex.Matches(input)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] res [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] rm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Text.RegularExpressions.Match [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] regmats[/SIZE]
[SIZE=2]res += [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](rm.Value)[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2]MsgBox(res.ToString)[/SIZE]
 
I do that in all code examples, but it's never the intention that these results are outputted in message boxes, just gives readers a good clue on what the target of the code is - where and how to retrieve key values. Think of it as a conclusive marker :)
 
Back
Top