Get only numbers from a string in a file

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

This line looks like this:

TimeAcquired=1433293042

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

So the output is:

1433293042

      

I just need numbers.

Is there a way to do this?

+3


source to share


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


Follow these steps:



  • read the full line
  • split the string into character =

    usingstring.Split()

  • extract second field of string array
  • convert string to integer using int.Parse()

    orint.TryParse()

+7


source


string setting=sr.ReadLine();
int start = setting.IndexOf('=');
setting = setting.Substring(start + 1, setting.Length - start);

      

+4


source


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


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


try

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

      

After a simple analysis.

int mrt= int.parse(t);

      

+2


source







All Articles