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);
C#
is better thanC++
...(~.~)
– Helmuth