Get only numbers from a string in a file
6 answers
There is a very simple way to do this, and that is call Split()
in line and take the last part. For example, if you want to store it as a string:
var myValue = theLineString.Split('=').Last();
If you need it as an integer:
int myValue = 0;
var numberPart = theLineString.Split('=').Last();
int.TryParse(numberPart, out myValue);
+6
source to share
A good approach to Extract Numbers Only
wherever they were found would be like this:
var MyNumbers = "TimeAcquired=1433293042".Where(x=> char.IsDigit(x)).ToArray();
var NumberString = new String(MyNumbers);
It's good when the FORMAT of the string is unknown. For example, you don't know how numbers were separated from letters.
+3
source to share
you can do it with split () function like below
string theLineString="your string";
string[] collection=theLineString.Split('=');
so your string is split in two, i.e.
1) part before "=" 2) part after "=".
so you can access the parts by their index.
if you want to access numeric just do this
string answer=collection[1];
+3
source to share