When I need to stringify some values by joining them with commas, I do, for example:
string.Format("{0},{1},{3}", item.Id, item.Name, item.Count);
And have, for example, "12,Apple,20"
.
Then I want to do opposite operation, get values from given string. Something like:
parseFromString(str, out item.Id, out item.Name, out item.Count);
I know, it is possible in C. But I don't know such function in C#.
Yes, this is easy enough. You just use the
String.Split
method to split the string on every comma.For example:
Use Split function
Possible implementations would use String.Split or Regex.Match
example.
or
Edit: Finally, SO has a bunch of thread on scanf implementation in C#
Looking for C# equivalent of scanf
how do I do sscanf in c#
If you can assume the strings format, especially that
item.Name
does not contain a,
This will simply do what you want but I would suggest you add some error checking. Better still consider serializing/deserializing to XML or JSON.