[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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
0
koo9
Asked:
koo9
  • 5
  • 5
  • 2
  • +1
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
weareuCommented:
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
 
weareuCommented:
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
 
weareuCommented:
and obviously don't forget to close sr:

so at the end:
sr.Close();
0
 
weareuCommented:
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
 
weareuCommented:
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now