Smurfwow Posted February 21, 2003 Posted February 21, 2003 Hi, I've been looking through the docs, but i havn't 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. Quote
*Gurus* divil Posted February 21, 2003 *Gurus* Posted February 21, 2003 I guess Regular Expressions provide the most advanced capability for parsing data out of strings, I can't think of anything really closer to what sscanf does. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted February 21, 2003 *Experts* Posted February 21, 2003 String.Format will do the trick. It uses named parameters, such as {0}, {1}, etc. Check the help, but here's a sample: Debug.WriteLine(String.Format("Hello {0}. You are {1} years old", "Dan", 31)); -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* divil Posted February 21, 2003 *Gurus* Posted February 21, 2003 That's more than sprintf than sscanf, though. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted February 21, 2003 *Experts* Posted February 21, 2003 Ah, I didn't read it close enough. You were right the first time, regular expressions with the Matches collection. :) -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Smurfwow Posted February 22, 2003 Author Posted February 22, 2003 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. Quote
*Gurus* divil Posted February 22, 2003 *Gurus* Posted February 22, 2003 You would have to use string pattern matching and manually convert the strings to their proper types. I don't know much about regular expressions so that's about all the help I can give I'm afraid. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted February 23, 2003 *Experts* Posted February 23, 2003 If indeed every piece of info is separated by a space, there's you regular expression match. As divil said, it won't 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 - I'd take a look at the help file on them for some basic info to get started. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.