How to extract the digits from the string

Hi All.I have a string like ‘10 or 10%’.Here The question is How to extract the exact numerical value from the string with out using any spilt string activity.The result I have to get is 10.Can anybody suggest me.

Hi,
you could try Split method of String

e.g. like this cint((“10 or 10%”).Split(" "c)(0))

Cheers

EDIT: Ignore. I did not notice the “without split…” :frowning:

Hi, you can use like this file
Main.xaml (5.6 KB)
It works

name = “10 or 10%”.Substring(0,2)
Like this you will get first and second position.
Regards

I have a number of strings like
1.10 or 10%
2.10%
3.1256&
4.187PHP
Please suggest how to extract only numbers from the string

Hi,
in such case regular expression.
e.g. ^([0-9]*)

Cheers

1 Like

@sandhyak9,

Use this Regular expression to identify numbers only in a given string: '\d’

Use this regex code. This will extract only the numbers present inside the string.
System.Text.RegularExpressions.Matches(inputstring,"([0-9]+)")

1 Like

Can you explain how to extract numerical value from the following string.

‘0.001 or 0.001$’.And the result should be 0.001

Use this regex code. This will extract the decimal numbers present inside the string.
System.Text.RegularExpressions.Matches(inputstring,"[0-9]+(.[0-9]+)")

1 Like

Hi @Karthick_Settu.Code is ok.But result is not storing in any datatype

You can store it in a variable IEnumerable(System.Text.RegularExpressions.Match). In this you can use index to fetch the value and use “.toString” or “.value” to retreive the value.

I cant use index here.Because the string values are dynamic here.For String values are like,
1.10 or 10%
2.10%
3.1000$
4.0.001$
5.0.001 or 0.001$

Results Should be like
1.10
2.10
3.1000
4.0.001
5.0.001

You can use like this:
System.Text.RegularExpressions.Matches(inputstring,"[0-9]+(.[0-9]+)")(0).ToString

@sandhyak9 You can use regex like this

Sorry to say.This code is not working.Code format is like this.
System.Text.RegularExpressions.Regex.Matches(SalaryChange,"[0-9]+(.[0-9]+)")(0).ToString.
And I’m getting Argument out of range exception

Can you try this Expression

\d+[.]\d+

Try this
System.Text.RegularExpressions.Matches(inputstring,"[0-9]\d*(.[0-9]\d+)?")(0).ToString

1 Like

Thank you so much @Karthick_Settu.This code is working.
And I modified the code like
System.Text.RegularExpressions.Regex.Matches(inputstring,"[0-9]\d*(.[0-9]\d+)?")(0).ToString

1 Like

Hi @Karthick_Settu,
For this expression, System.Text.RegularExpressions.Regex.Matches(inputstring,"[0-9]\d*(.[0-9]\d+)?")(0).ToString.
The following string values are not working.

  1. -2
  2. 0.5
    The results should be like
  3. -2
  4. 0.5
    Can you please optimize the line of code to make it feasible for these string values too.