I'm wondering if there's a built in way in .NET to parse bits of a string.
Take for example I have the following string:
"bsarbirthd0692"
made up of the following parts that will be cross referenced to data later:
Indexes Purpose
0-3 (name)
4-9 (description)
10-13 (date mm-yy)
I'm hoping for something native like:
string name, desc, date;
string.ParseFormat("{0:4}{1:5}{2:4}", "bsarbirthd0692", out name, out desc, out date);
Is there a native way to do this in .NET or a popular library?
No, there's no built-in way. I would use string methods like
Substring
:You can use Regexp for this
There is nothing like that, however writing something to implement:
with signature:
is very easy:
(With a real implementation having some error checking.)
NB. I've dropped the format string like interface: it appears to offer no value. Once the collection is returned it is easy to assign entries by index.
Since a format is known, and shouldn't change Substring should work for you
EDIT
There's also extension methods you can create to do what ever you want. This is obviously more complex than previous suggestion
Usage 1:
Result:
Usage 2:
Results:
Better from what? Something like this?