Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the value of Digits in a string? 1

Status
Not open for further replies.

jake007

Programmer
Jun 19, 2003
166
US
I have strings similar to the following ;

"btn12"
"btn13"
"btn14"

How would i be able to just get the numerics out of the string as a value, so "btn12" would be = to 12 and so on?

Jake
 
If the numeric part of your string is always composed of the 2 last characters, then you can retrieve the 2 last characters of the string then convert them to an integer.
I say 2 characters as an example but it's up to you to decide of course.

example:

Code:
dim myString as string = "btn12"
dim integerString as string

integerString = myString.Substring(myString.length - 2)

dim myInteger as integer = cint(integerString)
 
... and if that isn't the case?

RegEx looks like the easiest (and probably most flexible) approach.


Hope this helps.

[vampire][bat]
 
... and if it isn't the case you can still use a function that could look a little bit like this :

Code:
    Private Function GetNumericPart(ByVal MyString As String) As Integer

        'the string that'll store the numeric part
        Dim resultString As String = String.Empty
        'set the default value in case MyString doesn't end with a numeric part
        Dim defaultValue As Integer = 0

        'Loop through the characters starting from the end
        For i As Integer = MyString.Length - 1 To 0 Step -1

            ' if the character is numeric
            If IsNumeric(MyString(i)) Then

                'then store it 
                resultString = MyString(i) & resultString

                'else leave
            Else : Exit For

            End If

        Next

        'return the numeric part or the defaultValue
        If IsNumeric(resultString) Then
            Return CInt(resultString)
        Else
            Return defaultValue
        End If

    End Function
 
Mastakilla, why go to all that trouble when a very very simple regex will do it?

[vampire][bat]
 
I'm not a regex guru at all, I know how to use basic regex for input/display masks but not for analyzing strings and returning values so ... I use workarounds that serve me well untill I find a faster/smarter/easier way to do things.

I think it could be quite nice and educative for people like me [tongue] if you showed us how to let regex do the job for us.
 
I wonder if the site will stay up long enough to post an answer this time - they say third time lucky !!

Two variations, the first will only return the first number found, whereas the second will find all numbers:

Code:
Imports System.Text.RegularExpressions

Public Class Form1
  Inherits System.Windows.Forms.Form

'Windows stuff omitted

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    TextBox3.Text = Regex.Match(TextBox2.Text, TextBox1.Text).ToString

  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    For Each m As Match In Regex.Matches(TextBox2.Text, TextBox1.Text)
      TextBox3.Text += m.ToString + ", "
    Next
    TextBox3.Text = TextBox3.Text.Substring(0, TextBox3.Text.Length - 2)

  End Sub
End Class

In both cases:
TextBox1 contains [tt]\d+[/tt]
TextBox2 contains [tt]btn12btn13btn14[/tt]

The first returns: [tt]12[/tt]
The second returns: [tt]12, 13, 14[/tt]



Hope this helps.

[vampire][bat]
 
Thanks. I think I'll start using Regex more often, you've convinced me.

 
Some RegEx expressions can be so complicated to put together, that the time taken to work out the match pattern is simply not worth the effort when other alternatives are available - but in this case it was so straightforward as to make any other method redundant. [wink]

As an aside, there are several websites that list "useful" patterns - but always check them yourself against your data, they are not always as accurate as those posting them would have you believe. I've been caught several times, so I tend either not to use them or to use them as a starting point - but in any event they do provide good learning references.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top