We help IT Professionals succeed at work.

parsing a string variable and get the numbers

87 Views
Last Modified: 2019-02-21
I'm using visual basic .net in a aspx web forms application

If I have a string variable that looks like this that contains numbers and characters.

How do I save only the numbers in a different variable called Intvariable1 ?

Testvariable1 = "123|YYY"

sometimes the values in TestVariable1 will be this "123|YYY"  or this "12|YYY"  or this "3|YYY"
Comment
Watch Question

it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
If that is your value set, then I would split on the pipe and TryParse the splits as a numeric type; e.g. -
Module Module1
    Sub Main()
        Dim values = New String() {"123|YYY", "12|YYY", "3|YYY"}
        For Each value In values
            Dim result As Integer
            Dim selects = (From split In value.Split(New String() {"|"c}, StringSplitOptions.RemoveEmptyEntries)
                           Where Integer.TryParse(split, result)
                           Select split)
            For Each selected In selects
                Console.WriteLine($"{value} contains {selected}")
            Next
        Next
        Console.ReadLine()
    End Sub
End Module

Open in new window

Which produces the following output -Capture.PNG-saige-

Author

Commented:
On this link
https://stackoverflow.com/questions/13356018/get-only-numbers-from-string

I saw this example:

resultString = Regex.Match(subjectString, @"\d+").Value;


if subjectstring = "28|NN"

then it returns 28

But it only works for positive numbers.

Because if subjectstring = "-1|NN"

then it returns 1

How do fix regex example to also work with negative numbers?
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
thanks. Both great examples!
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome, glad that was useful.


ยปbp

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions