Extract Decimal Value From A String - vb.net-2010

I have 12.00 Php as a string and I'd like to get 12.00 only. Can someone please help me with this? I find it hard to Google for this. TryParse or any conversion doesn't work, or return 0.
Dim i As String = "12.00Php"

Dim regex As Regex = New Regex("\d+.\d+")
Dim match As Match = regex.Match("12.00php")
If match.Success Then
Console.WriteLine(match.Value)
End If

Related

Get a particular string from the main string in VB.net

substring = "frTrig_worldcup"
main string = "0.10/BMM/fTrig_Mast/BOYU/frTrig_worldcup"
The substring value will change. "frTrig_xxxx" but "frTrig_" will be constant
I need to get the substring which is in the LAST PART of the main string. The issue is the "frTrig_" comes in mid of the string also
Please help me to solve this.
We can try using String#split here:
Dim input As String
Dim parts() As String
input = "0.10/BMM/BOYU/fTrig_MastrrRea0.01/frTrig_worldcup"
parts = input.Split("/")
MsgBox(parts(parts.Length-1))

Vb.net, replace any number at the end of a string with a placeholder

I have many strings that have numbers at the end. The numbers can be of any size, for example:
myvar123
mysecondvar3
mythirdvar219107
The strings can have numbers even inside the name, not only at the end.
for example:
my2varable123
some123variable9480395
I would need to replace any number at the END with a placeholder. (NOT those inside the varname.)
for example:
my2varable123 should become: my2variable%placeholder%
some123variable9480395 should become: some123variable%placeholder%
The only way that comes to my mind is to go through the string using .right() and remove the char if it is numeric until I find the first non numeric char. Then in the end append the placeholder, but it looks like a lot of work for a rather simply problem.
Is there a better way to do this?
You could use a Regex for this.
Dim str As String = "some123variable9480395"
Dim pattern As String = "\d+$"
Dim replacement As String = "%placeholder%"
Dim rgx As Regex = New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)

How to split decimals and charecter from a string

I need a bit help.
I want to split decimal and character in a string.
Eg : 0.5Lg ---> 0.5 separate and Lg separate.
0.22Ldd --->0.22 separate and Ldd Separate
I tried Following:
Dim input As String = "0.22Ldd"
Dim pattern As String = "[^0-9\.]+"
Dim substrings() As String = Regex.Split(input, pattern)
TextBox11.Text = substrings(0)
This gives output of 0.22 but how to extract Ldd part?
I tried replacing pattern with "\D+" and "\d+" and "\W+" and [a-zA-Z]+ etc etc after googling but no luck.
Can somebody help. I have googled a lot either output comes along with dot or along with number.
You can use this LINQ approach:
Dim input As String = "0.22Ldd"
Dim numericPart = input.TakeWhile(Function(c) Char.IsPunctuation(c) orelse Char.IsDigit(c)).ToArray()
Dim number as Decimal
Dim validNumber = Decimal.TryParse(new String(numericPart), NumberStyles.Any, NumberFormatInfo.InvariantInfo, number)
Dim letterPart As String = input.Substring(numericPart.Length)
if you want to split them with regex you can try pattern below. Regex.Split gives you matching patterns so you should check for number pattern and text pattern in single regex expression.
Dim pattern As String = "([a-zA-Z]*)([0-9]*[.]?[0-9]+)"

How to get value from expression in vb.net

I have the following string expression:
str="1+2+3+4"
Now from this string I want the value of number (10). How can I get this value from this expression?
For that to work, you'd have to parse all the numbers, something like this
Dim str As String= "1+2+3+4"
Dim numbers() As String = str.Split('+')
Dim result As Integer = 0
For Each number As String In numbers
result += Integer.Parse(number)
Next
Here is an alternative solution that uses Linq. This solution excludes nothing it can not parse...
YOURSTRING.Split("+").ToList.Where(Function(sr) Not String.IsNullOrEmpty(sr) AndAlso Integer.TryParse(sr, New Integer)).Sum(Function(s As String) Integer.Parse(s))
Example Output
1+2+3+4 = 10
1+2+3+4++12+1 = 23 (Notice the typo (4++12), it still works even it there's a mistake)

Remove the character alone from the string

My code retrieves the data from various resources .
And output will be like below
UNY4/4/2010
hds04/5/2010
saths04/22/2013
But I want the output like this
4/4/2010
4/5/2010
04/22/2013
Is there any way to do this ?
You need to use a regular expression that finds all uppercase and lowercase characters and replaces them with a blank, like this:
Dim rgx As New Regex("[a-zA-Z]")
str = rgx.Replace(str, String.Empty)
An alternate solution is to look for the first numeric digit, then discard all text before that.
Function GetDate(data As String) As Date
Dim indexFirstNum As Integer = data.IndexOfAny("0123456789".ToCharArray())
Dim datePortion As String = data.Substring(indexFirstNum)
Return Date.Parse(datePortion)
End Function