Is the re an easy way to pluck out the set of digits from a textbox entry?-VBForums
Results 1 to 7 of 7

Thread: Is the re an easy way to pluck out the set of digits from a textbox entry?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2017
    Posts
    47

    Is the re an easy way to pluck out the set of digits from a textbox entry?

    Two sets of data in a line are separated using a "-" as follows: XXXX - ###, or XXXXXX - ##, or even XX - ###
    The number of digits at the end can change from 1 to 4. The hyphen is a separator.

    Is there a single line of code that will see the hyphen then pull out the rest of the line to the right of the hyphen?

    I am considering reading the number of digits in the phrase, counting to the hyphen, subtract the two then grab that number of digits after the hyphen. Too many lines of code.

    Is there a singe command to do this?

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,615

    Re: Is the re an easy way to pluck out the set of digits from a textbox entry?

    Try using String.Split("-")

    If you can guarantee the hyphen exist and there is only one you can use it directly,
    Code:
    Dim numbers as string = myTextBox.Text.Split("-")(1)
    If you can't make those assumptions, then you will need to test how many array elements are returned from the split method

    Code:
    Dim parts() as string = myTextBox.Text.Split("-")
    Select case parts.count
    case <2 'no hyphen
    case =2 'one hyphen
    case else 'multiple hyphens
    Of course you said you want a single line, so for that you will need to use linq, which I'm afraid I cannot help with.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Addicted Member
    Join Date
    Sep 2017
    Posts
    137

    Re: Is the re an easy way to pluck out the set of digits from a textbox entry?

    If you only want the numbers you could iterate the chars

    Code:
            Dim OldString As String = "gerjhvwe-546541"
            Dim NewString As String = ""
            For Each c As Char In OldString
                If IsNumeric(c) Then
                    NewString &= Asc(c)
                End If
            Next
            Dim NewInt As Integer = CInt(NewString)
    You could also use Regular Expressions I am sure

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2017
    Posts
    47

    Re: Is the re an easy way to pluck out the set of digits from a textbox entry?

    The line:

    Dim numbers As String = txtLineOfText.Text.Split("-")(1)

    works but either Option Explicit or Option Strict doesn't like it but I am not all that concerned for the moment.

    Thanks for the quick response.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,934

    Re: Is the re an easy way to pluck out the set of digits from a textbox entry?

    Here is some code with test data. The one liner is the line with <<<<<<<<<<<<<<<<<

    Code:
            Dim testData As XElement = <test>
                                           <l>XXXX - 123</l>
                                           <l>XXXXXX - 89</l>
                                           <l>XX - 456</l>
                                       </test>
    
            For Each el As XElement In testData.Elements
                Dim theLine As String = el.Value
                If theLine.Contains("-") Then
                    Dim restOfLine As String = theLine.Substring(theLine.IndexOf("-"c) + 1) '.Trim '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    Stop
                End If
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,375

    Re: Is the re an easy way to pluck out the set of digits from a textbox entry?

    Quote Originally Posted by GarySut View Post
    The line:

    Dim numbers As String = txtLineOfText.Text.Split("-")(1)

    works but either Option Explicit or Option Strict doesn't like it but I am not all that concerned for the moment.
    It would be Option Strict (which is about data type conversions), not Option Explicit (which forces you to declare your variables).

    This minor correction (which indicates that the data type of "-" is Char) should remove the warning:
    Code:
            Dim numbers As String = txtLineOfText.Text.Split("-"c)(1)

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2017
    Posts
    47

    Re: Is the re an easy way to pluck out the set of digits from a textbox entry?

    Got it, the addition of "c" fixed it. I was trying to use the "help' screens but...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.