sscanf - is there a .net equivilent? (or a good way to parse strings?)

Smurfwow

New member
Joined
Feb 4, 2003
Messages
4
Location
Sydney
Hi,

Ive been looking through the docs, but i havnt been able to find anything which can do what the C function sscanf() does.
(please dont confuse with scanf(), which is a fileio function. sscanf is a string parsing function)

Does anyone know if there is a class that can take a format, a string and some objects, and put the values it finds (according to the format) in the string, in the objects? (exactly what sscanf does)

Thanks.
 
I guess Regular Expressions provide the most advanced capability for parsing data out of strings, I cant think of anything really closer to what sscanf does.
 
String.Format will do the trick. It uses named parameters, such as {0}, {1}, etc. Check the help, but heres a sample:

C#:
Debug.WriteLine(String.Format("Hello {0}. You are {1} years old", "Dan", 31));

-Nerseus
 
Ah, I didnt read it close enough. You were right the first time, regular expressions with the Matches collection. :)

-ner
 
ok... are there .net regular expressions to match the value types?
(to provide the functionality to match sscanf :P)

so, for example, if i had a string which was made up of, say; a string, an int32, a single, another int32, and a double
(eg "hello 45626 0.63735 463573 1654.2563656464"), would i be able to match it with something like "%s %i %f %i %d" ?

(cause when i looked in the docs, i couldnt find a page that listed regular expressions which allowed for the use of value types, i could only find string pattern matching stuff)

Thanks.
 
You would have to use string pattern matching and manually convert the strings to their proper types. I dont know much about regular expressions so thats about all the help I can give Im afraid.
 
If indeed every piece of info is separated by a space, theres you regular expression match. As divil said, it wont do type conversions automatically but if you know the type in C/C++ (%f %s, etc.) then you can call the appropriate Convert.Toxxx function or whatever you like (Int32.Parse(), etc.). Regular expressions can match in other ways than just spaces, of course - Id take a look at the help file on them for some basic info to get started.

-Nerseus
 
Back
Top