8 Replies - 33617 Views - Last Post: 25 January 2013 - 03:41 AM Rate Topic: -----

#1 Wanting2Learn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 08

Extracting Numbers from String Text

Posted 01 February 2009 - 09:31 AM

I am trying to extract numbers from string text.

e.g.
1 day (8 hours) ' I need the one and the eight integers pulled out

I would like to extract then store each number separately for use later.

I am aware of techniques to count string text, but not pulling certain information from it.

Any help would be appreciated.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Extracting Numbers from String Text

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: Extracting Numbers from String Text

Posted 01 February 2009 - 10:10 AM

Are you wanting to store each number individually or as actual numbers like if you ran across 12 days do you want it stored as 1, 2 or the number 12?

If you are wanting to store individually, look at the Char class and how it has a isDigit() method which takes in a character (loop through the string character by character) and compare it to see if it is a digit. Then you can save it.

If you are looking to store numbers together, as in the number 12, then you can use a regular expression that matches a pattern of 1 or more digits... something along the lines of /\d+/.

So give those a try and then show us your code so we can see where you are going wrong. :)
Was This Post Helpful? 0
  • +
  • -

#3 Wanting2Learn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 08

Re: Extracting Numbers from String Text

Posted 01 February 2009 - 02:19 PM

Thanks for the reply. I am stuck like chuck. I keep getting infinity errors and string to integer errors.
I have been looking at this so long it must be something simple

Public Class Form1
	Public Function FindAllNumbers(ByRef strIn As String) As Integer

		Dim intY As Short
		Dim intX As Short

		For intY = 1 To Len(strIn)

			intX = Asc(Mid(strIn, intY))
			If intX >= 48 And intX <= 58 Then
				FindAllNumbers = CInt(FindAllNumbers & Mid(strIn, intY, 1))
			End If
		Next intY

	End Function

	Private Sub cmdVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVerify.Click
		Dim textStringInput As String
		Dim integerOutput As Double

		textStringInput = txtStr.Text
		Call FindAllNumbers(textStringInput)
		integerOutput = CInt(textStringInput)
		lblDisplay.Text = integerOutput
	End Sub

	Private Sub lblDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDisplay.Click

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#4 Wanting2Learn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 08

Re: Extracting Numbers from String Text

Posted 01 February 2009 - 02:34 PM

I am trying to store the groups of numbers seperately - Sorry i forgot to add that earlier
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: Extracting Numbers from String Text

Posted 01 February 2009 - 02:48 PM

Well then if you want to save each individual number in the string, you just need to setup something like this...

' Here is my string of text
Dim mytext As String = "1 day (18 hours)"

' Convert it to an array of characters
Dim myChars() As Char = mytext.ToCharArray()

' Loop through the array testing if each is a digit
For Each ch As Char In myChars
     If Char.IsDigit(ch) Then
          ' If it is a digit, show it in a messagebox
          ' (here is where you would save the number to array, variable or whatever)
          MessageBox.Show(ch)
     End If
Next



We take our string, convert it into a character array, loop through the array testing each to see if it is a digit, and if it is then we put it in a messagebox to show the number.

Where we use the messagebox you could store the number as you like. Whether it be in an array of numbers or to a file or to a variable etc.

The example above will then show three message boxes... 1.... 1... 8.

Hope you get the idea.

"At DIC we be number plucking code ninjas... we also pluck death from the air and give it to our enemies as a gift." :snap:

This post has been edited by Martyr2: 01 February 2009 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#6 Wanting2Learn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 08

Re: Extracting Numbers from String Text

Posted 07 February 2009 - 11:57 AM

Thanks for the help
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Extracting Numbers from String Text

Posted 07 February 2009 - 12:06 PM

You could use Regular Expressions (as already pointed out), then create a MatchCollection to hold each value that matches what you're looking for, numeric values in this case

Private Shared Function Strip(ByVal value As String) As Integer
	Dim returnVal As String = String.Empty
	Dim collection As MatchCollection = Regex.Matches(value, "\d+")
	For Each m As Match In collection
		returnVal += m.ToString()
	Next
	Return Convert.ToInt32(returnVal)
End Function


Was This Post Helpful? 1

#8 Guest_cesarjusto*


Reputation:

Re: Extracting Numbers from String Text

Posted 15 July 2010 - 02:59 PM

Regex.IsMatch(AnyStringWithNumbers, "\d")

it returns true if theres is a number within the evaluated string
Was This Post Helpful? 0

#9 Ruddy_codehunger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-January 13

Re: Extracting Numbers from String Text

Posted 25 January 2013 - 03:41 AM

Public Function ValidateNumber(ByVal a_strNumberString As String) As Boolean

Dim var_BoolValidate as Boolean
var_BoolValidate =True
For x As Integer
If Asc(OurString.Char(indexOfChar))<48 Or Asc(OurString.Char(indexOfChar)) > 57 Then
     var_BoolValidate =False
End If
Next
ValidateNumber=var_BoolValidate 
End Function


View Postcesarjusto, on 15 July 2010 - 02:59 PM, said:

Asc can also be used like
finally, returning bool val will decide that whether it contains all the digits or not


Public Function ValidateNumber(ByVal a_strNumberString As String) As Boolean

Dim var_BoolValidate as Boolean
var_BoolValidate =True
For x As Integer
If Asc(OurString.Char(indexOfChar))<48 Or Asc(OurString.Char(indexOfChar)) > 57 Then
     var_BoolValidate =False
End If
Next
ValidateNumber=var_BoolValidate 
End Function


Quote

Asc can also be used like above

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1