|
| | >
| > | > |
message
| > | | > | >
| > | > | > | > | Hello,
| > | > | Anybody know if anything exists like sscanf in c.
| > | > | I found a few things OL but most were pretty old. Maybe
something
| > has
| > | > | come along since 2004?
| > | > | Thanks
| > | > | Mike
| > | > |
| > | >
| > | > Ignore my previous post.
| > | > Using Parse and TryParse methods you can achieve the same (and
more)
| > | > results
| > | > as sscanf in C.
| > | > Consider following sample....
| > | >
| > | > static void Main()
| > | > {
| > | > string tokenString = "12 25 56 4";
| > | > string [] split = tokenString.Split(new Char [] {' '});
| > | > int Int32Val;
| > | > char charVal;
| > | > float floatVal;
| > | > bool result = Int32.TryParse(split[0], NumberStyles.Integer,
| > null,
| > | > out
| > | > Int32Val);
| > | > if(result)
| > | > Console.WriteLine(Int32Val);
| > | > result = Char.TryParse(split[1][0].ToString(), out charVal);
| > | > if(result)
| > | > Console.WriteLine(charVal);
| > | > result = Single.TryParse(split[2], NumberStyles.Float, null,
out
| > | > floatVal);
| > | > if(result)
| > | > Console.WriteLine("{0:f}", floatVal);
| > | > }
| > | >
| > | >
| > | > This should output:
| > | >
| > | > 12
| > | > 2
| > | > 56,00
| > | >
| > | > Willy.
| > | >
| > | >
| > | >
| > | >
| > |
| > | Well, suppose you have the following string:
| > |
| > | "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
| > |
| > | With sscanf, I believe you can do something like:
| > |
| > |
| > | sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1,
value2,
| > | value3);
| > | printf("Sub-Total: %d", value3);
| > |
| > |
| > | Using regex, you can do something similar...and parsing out yourself
| > would
| > | be more trickier...
| > |
| > | HTH,
| > | Mythran
| > |
| >
| > I prefer using TryParse over RegEx, just a matter of taste, and quite
| > faster
| > ;-)
| >
| > // suppose the current culture is en-US...
| > Decimal decVal;
| > string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
| > $65.07";
| > string [] split = tokenString.Split(new Char [] {' '});
| > bool result = Decimal.TryParse(split[7], NumberStyles.Currency,
null,
| > out decVal);
| > if(result)
| > Console.WriteLine("Sub-total: {0:c}", decVal);
| >
| > Not really tricky IMO.
| >
| > Willy.
| >
| >
|
| grr, that's not my point. The OP requested similar functionality to
sscanf.
| The closest he can get is by using Regex...sure, you can parse it
yourself
| into an array and access the individual elements, but that's not what
the
OP
| was originally asking AFAIK (even though, it should work for the OP).
| Anywho

|
| Mythran
|
Sorry, but I'm affraid we'll have to agree to disagree. I don't see how a
simple C library 'function' like sscanf compares to Regex, a 'library' and
a
complex engine on it's own, it's too heavy weight compared to sscanf.
I know you can achieve the same using RegEx, but I can do exactly that
using
some minimal code arround the individual type's Parse and TryParse
methods.
Notice, that the OP asks about sscanf, this means he has a C background,
chances are that he know little/nothing about RegEx, maybe he will prefer
to
use Parse and TryParse, the choice will be on him.
Willy.
|
I do see where you are coming from. And yes, I will continue to disagree as
we have previously agreed to allow

On second thought, maybe I will agree
with you about parsing out manually depending on the amount of data the OP
needs to parse. If it is a larger amount of data, and performance isn't an
issue, then I would then go with Regex for this.
Mythran