How to extract int/double from a string?

hi all

in a console app, I need to parse the intput string from a text file then extract numeric value from it.
what's the easiest way to do it?

thx


koo9
koo9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

acharbonneauCommented:
double d;
string s;

d = double.parse(s)
//d now contains the numeric value of s


0
koo9Author Commented:
sorry, I need to parse a string containing more than one value.

well, I guess I can do sourcestring.split(some_delimiter) to get the string[], then check the string array content one by one. I was hoping that there is a faster and easier way to do this.


thx anyway.

koo9
0
koo9Author Commented:
regular expression seems might take soemtimes to learn and I think it's for this kind of operation. but haven't look deep into it.

koo9
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

weareuProgrammerCommented:
Please explain what you are trying to extract from the text file -> the format of the this text file and we could give you a RegEx to extract the information...
0
weareuProgrammerCommented:
otherwise:

FileInfo finfo = new FileInfo("mytextfile.txt");
StreamReader sr = finfo.OpenText();
string contents = sr.ReadToEnd();

string mynum = "";
foreach(char ch in contents)
{
  if(Char.IsNumber(ch))
  {
     mynum += ch.ToString();
  }
}

Console.WriteLine(mynum);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weareuProgrammerCommented:
and obviously don't forget to close sr:

so at the end:
sr.Close();
0
weareuProgrammerCommented:
Oh, and 4 the record RegEx is not easy to use... DEFFINITELY NOT!
Allthough I am quite familiar with the syntax, for complex string manipulation it still gets hectic to write and/or debug...
0
weareuProgrammerCommented:
If you insist on a RegEx, yes, please provide the format, if it's simple enough then it should be easy enough...
0
msdixonCommented:
here's the regular expression. really easy. just \d+

using System.Text.RegularExpressions;

Regex regex = new Regex(
    @"\d+",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.Compiled
    );

// this will give you the collection of matches
//regex.Matches(inputString);

iterate through it like this:

         foreach (Match match in regex.Matches(inputString))
         {
            // here's the number
            //match.Value
         }
0
koo9Author Commented:
weareu

thanks for the responses. I think i will go without the regex, it seems to be a pain to use. plus it need times to learn, it's not a bad thing to learn in the future i think but not for now.

koo9
0
koo9Author Commented:
i will give you the credit anyway.
0
msdixonCommented:
i disagree about regular expressions being horribly difficult. once you get the hang of it, it's pretty easy to look at it bit by bit. here's a good app i use all the time:
http://www.codeproject.com/dotnet/expresso.asp
0
koo9Author Commented:
thx, i will look into it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.