C# equivalent of C sscanf [duplicate]
Asked Answered
T

5

29

Possible Duplicate:
Is there an equivalent to 'sscanf()' in .NET?

sscanf in C is a nice way to read well formatted input from a string.

How to achieve this C#.

For example,

int a,b;
char *str= "10 12";
sscanf(str,"%d %d",&a,&b);

The above code will assign 10 to a and 12 to b.

How to achieve the same using C#?

Travistravus answered 19/11, 2010 at 10:2 Comment(1)
And some people at Microsoft think C# is better than C++ ... (~.~)Helmuth
P
18

There is no direct equivalent in C#. Given the same task in C#, you could do it something like this:

string str = "10 12";
var parts = str.Split(' ');
int a = Convert.ToInt32(parts[0]);
int b = Convert.ToInt32(parts[1]);

Depending on how well-formed you can assume the input to be, you might want to add some error checks.

Prognostication answered 19/11, 2010 at 10:10 Comment(4)
I am aware of this technique already. It would have been nice though to have something like sscanf in C#.Travistravus
@Gunner, I agree it would be nice. But I think you would have to implement it yourself, if you need it. It doesn't exist in the framework.Prognostication
downvote; this is a ridiculous answer. sscanf is vastly more complex than simply splitting on a common delimiter. For example, "%d%3s%d" should resolve "123abc567" to 123, abc, 567, which is something that split cannot be used for.Cahan
@Doug, I am well aware that sscanf is a different beast from Split, hence I start the answer by "There is no direct equivalent" -> And then I give an example on how the concrete task mentioned by the OP can be accomplished in C#.Prognostication
B
11

There is no direct equivalent of sscanf in the .NET Framework.

The simplest way to achieve the same functionality is splitting the string (String.Split) and then assigning the subsequent parts to variables with the Int32.Parse method. For example:

string myString = "10, 12";
string[] stringValues = myString.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
int a = Int32.Parse(stringValues[0]);
int b = Int32.Parse(stringValues[1]);

Many different data types in the Framework have Parse methods, including enumerations, if the values you want to read in from the string are not necessarily integer values.

You could also use regular expressions, but they're probably a bit overkill for a task as simple as this.


EDIT: If you're truly deadset on using sscanf, you could always consider P/Invoking the function from the C runtime libraries. Something like this perhaps (untested):

[DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int sscanf(string buffer, string format, ref int arg0, ref int arg1);
Badinage answered 19/11, 2010 at 10:14 Comment(0)
S
9

I'm just writing to kill time.

public static unsafe void Main(string[] args)
{
    int a, b,c;
    string str = "10 12 100";
    sscanf(str, ' ', &a, &b, &c);
    Console.WriteLine("{0} {1} {2}", a, b, c);
    Console.Read();
}

public static unsafe void sscanf(string str, char seperator, params int*[] targets)
{
    var parts = str.Split(seperator);
    if (parts.Length != targets.Length) throw new ArgumentException();
    for (int i = 0; i < parts.Length; i++)
    {
        *targets[i] = int.Parse(parts[i]);
    }
}
Schauer answered 19/11, 2010 at 10:22 Comment(3)
i know you are just killing time. BUT your code is rockin! (very concise, very readable, very flexible --> equals rockin!)Rabb
i bet some people weren't up voting because of the "unsafe" thing. i am pretty sure most c# developers are not allowed to use the "unsafe" keyword. i am a very "c" centric developer so... no biggie there.Rabb
or just because it is only a part of sscanf functionality ;-)Clowers
I
8

Writing your own sscanf() method in C# isn't that difficult, if you don't mind writing a little low-level code.

You can see my version in the article A sscanf() Replacement for .NET.

Inexpiable answered 14/2, 2012 at 19:41 Comment(1)
it seems to fail to parse this format: "%[0-9]/%[0-9]/%[0-9]"Laure
P
1

from Run-Time Routines and .NET Framework Equivalents

sscanf, swscanf ==> See Parse methods, such as System.Double.Parse

So, I guess there's no direct equivalent.

Polytonality answered 19/11, 2010 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.