Re: sscanf in c#




"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
news:Owmw%23s32GHA.4932@xxxxxxxxxxxxxxxxxxxxxxx

"Mythran" <kip_potter@xxxxxxxxxxx> wrote in message
news:uSleo322GHA.1040@xxxxxxxxxxxxxxxxxxxxxxx
|
| "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
| news:eQCLID22GHA.1068@xxxxxxxxxxxxxxxxxxxxxxx
| >
| > "AMP" <ampeloso@xxxxxxxxx> wrote in message
| > news:1158593340.752398.293970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| > | 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


.



Relevant Pages

  • Re: sscanf in c#
    ... formatting it and storing it in blkout. ... |> | With sscanf, I believe you can do something like: ... |> I prefer using TryParse over RegEx, just a matter of taste, and quite ... some minimal code arround the individual type's Parse and TryParse ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sscanf in c#
    ... |> | With sscanf, I believe you can do something like: ... |> I prefer using TryParse over RegEx, just a matter of taste, and quite ... some minimal code arround the individual type's Parse and TryParse ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sscanf in c#
    ... |> | With sscanf, I believe you can do something like: ... |> I prefer using TryParse over RegEx, just a matter of taste, and quite ... some minimal code arround the individual type's Parse and TryParse ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sscanf in c#
    ... | needs to parse. ... This illustrates the equivalence of sscanf and Parse (or preferably: ... I would never use RegEx for this. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sscanf in c#
    ... |> | With sscanf, I believe you can do something like: ... |> I prefer using TryParse over RegEx, just a matter of taste, and quite ... some minimal code arround the individual type's Parse and TryParse ...
    (microsoft.public.dotnet.languages.csharp)