koo9
asked on
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
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
ASKER
sorry, I need to parse a string containing more than one value.
well, I guess I can do sourcestring.split(some_de limiter) 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
well, I guess I can do sourcestring.split(some_de
thx anyway.
koo9
ASKER
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
koo9
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and obviously don't forget to close sr:
so at the end:
sr.Close();
so at the end:
sr.Close();
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...
Allthough I am quite familiar with the syntax, for complex string manipulation it still gets hectic to write and/or debug...
If you insist on a RegEx, yes, please provide the format, if it's simple enough then it should be easy enough...
here's the regular expression. really easy. just \d+
using System.Text.RegularExpress ions;
Regex regex = new Regex(
@"\d+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.Compiled
);
// this will give you the collection of matches
//regex.Matches(inputStrin g);
iterate through it like this:
foreach (Match match in regex.Matches(inputString) )
{
// here's the number
//match.Value
}
using System.Text.RegularExpress
Regex regex = new Regex(
@"\d+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.Compiled
);
// this will give you the collection of matches
//regex.Matches(inputStrin
iterate through it like this:
foreach (Match match in regex.Matches(inputString)
{
// here's the number
//match.Value
}
ASKER
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
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
ASKER
i will give you the credit anyway.
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
http://www.codeproject.com/dotnet/expresso.asp
ASKER
thx, i will look into it.
string s;
d = double.parse(s)
//d now contains the numeric value of s