# [C#] extracting variables from strings

This topic is 4241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've just started using C#, and I can't seem to figure out how to extract variables from a string. I'm looking for something similar to c++'s sscanf, I guess. When I googled for help, most results were about extracting them manually, which I could take the time to do, but with c#'s huge high level library, I can't imagine there isn't a function designed for just such a purpose already. Any help is appreciated, Bender

##### Share on other sites
Most of the numeric class types have Parse methods that you can use to extract values from strings like Double.Parse() or Boolean.Parse()

##### Share on other sites
Thanks! I was afraid it would be something simple like that... Exactly what I was looking for

##### Share on other sites
Are you just wanting to read primitive types, like scanf, or something more complicated? (For the primitive types, the last poster already mentioned the solution for that).

OOPS... you responded to him before I posted. Cool, glad you got it working!

##### Share on other sites
What I'm actually working on is a routine to load .N3D files, so actually I think I need a little more... N3D files don't explicitly state the number of vertices. Each line containing a vertice starts with a v,('v 0.00000 0.00000 0.00000'). In c++ I would go through the file with sscanf and check the return value, so i knew how many instances there were. I dont really think I can do that with the .parse() functions sicrane was talking about. Any more ideas?

##### Share on other sites
Regular expressions.

##### Share on other sites
Thanks Arild, but I can't figure out a way to use them with floats...

##### Share on other sites
Okay, I figured out how to do it now... check if it fits the format 'v float float float' then split the string into 3 others and use float.parse(). Just one question, I googled this and its kind of long so i didnt really find any helpfull answers.

//I have this methodprotected void testFunction(string f){}//If I pass it a string declared like this...string testString = "Test!"; testFuction(testString);// It gives me this errorError	1	An object reference is required for the nonstatic field, method, or property

edit: Sorry I never get my point across lol. I simplified the code so it shows what i mean...

[Edited by - glBender on July 9, 2006 12:39:26 AM]

##### Share on other sites

string myString = "1";
int myInt = Convert.ToInt32(myString);

##### Share on other sites
Bump, I edited my post before this to clarify my question :)