Get only numbers from line in file

181 views Asked by At

So I have this file with a number that I want to use.

This line is as follows:

TimeAcquired=1433293042

I only want to use the number part, but not the part that explains what it is.

So the output is:

1433293042

I just need the numbers.

Is there any way to do this?

6

There are 6 answers

0
Steve Lillis On BEST ANSWER

There is a very simple way to do this and that is to call Split() on the string and take the last part. Like so if you want to keep it as a string:

var myValue = theLineString.Split('=').Last();

If you need this as an integer:

int myValue = 0;
var numberPart = theLineString.Split('=').Last();
int.TryParse(numberPart, out myValue);
0
DrKoch On

Follow these steps:

  • read the complete line
  • split the line at the = character using string.Split()
  • extract second field of the string array
  • convert string to integer using int.Parse() or int.TryParse()
1
apomene On
string setting=sr.ReadLine();
int start = setting.IndexOf('=');
setting = setting.Substring(start + 1, setting.Length - start);
1
Franco Pettigrosso On

try

string t = "TimeAcquired=1433293042";
t= t.replace("TimeAcquired=",String.empty);

After just parse.

int mrt= int.parse(t);
2
Transcendent On

A good approach to Extract Numbers Only anywhere they are found would be to:

var MyNumbers = "TimeAcquired=1433293042".Where(x=> char.IsDigit(x)).ToArray();
var NumberString = new String(MyNumbers);

This is good when the FORMAT of the string is not known. For instance you do not know how numbers have been separated from the letters.

0
hdkhardik On

you can do it using split() function as given below

string theLineString="your string";
string[] collection=theLineString.Split('=');

so your string gets divided in two parts, i.e.

1) the part before "=" 2) the part after "=".

so thus you can access the part by their index.

if you want to access numeric one then simply do this

string answer=collection[1];