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"
ASP.NETVisual Basic.NET.NET Programming
Last Comment
Bill Prew
8/22/2022 - Mon
it_saige
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 SubEnd Module
Open in new window
Which produces the following output -