Looking for C# equivalent of scanf
If regular expressions aren't working for you, I've just posted a sscanf()
replacement for .NET. The code can be viewed and downloaded at http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net.
C# equivalent of C sscanf
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.
c# equivalent of fscanf()
var parts = streamReader.ReadLine().Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
int x = Int32.Parse(parts[0]);
int y = Int32.Parse(parts[1]);
how do I do sscanf in c#
If like scannf you are willing to assume that users will give completely correct data then you can do the following.
string astring = ...;
string[] values = astring.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
int a = Int32.Parse(values[0]);
int b = Int32.Parse(values[1]);
int c = Int32.Parse(values[2]);
Regular expressions would work for this scenario but they are a bit overkill. The string can easily be tokenized with the aforementioned Split method.
c# equivalent of fscanf()
var parts = streamReader.ReadLine().Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
int x = Int32.Parse(parts[0]);
int y = Int32.Parse(parts[1]);
Related Topics
Publish a Project with Local Database
Ef Core 2.1 Hasconversion on All Properties of Type Datetime
Invoking Powershell Cmdlets from C#
C# - How to Copy a Single Excel Worksheet from One Workbook to Another
C# Encryption to PHP Decryption
Ghost-Borders ('Ringing') When Resizing in Gdi+
How to Check If an Ip Address Is Within a Particular Subnet
ASP.NET MVC Controller Lifecycle
How to Use Multi Color in Richtextbox
Combobox.Selectedtext Doesn't Give Me the Selectedtext
Convert Anonymous Type to Class
How to Add Property-Level Attribute to the Typedescriptor at Runtime
Selecting the Size of a System.Drawing.Icon