Reputation: 7590
I have a string "10/15/2010"
I want to split this string into 10, 15, 2010 using c#, in VS 2010. i am not sure how to do this. If someone can tell me what function to use, it would be awesome.
Thank you so much!!
Upvotes: 0
Views: 452
Reputation: 887433
You probably want to call
DateTime date = DateTime.Parse("10/15/2010", CultureInfo.InvariantCulture);
Upvotes: 10
Reputation: 29244
Or do like a saw in a recent program (in Fortran which I am translating to C# below) ..
string[] parts = "10/15/2010".Split('/');
if( parts[0] == "01" ) month = 1;
if( parts[0] == "02" ) month = 2;
if( parts[0] == "03" ) month = 3;
if( parts[0] == "04" ) month = 4;
...
you get the idea. It kills me when people code it something crazy instead of calling a built in function to do the same thing.
( please don't flag me down, this is just a joke, not a real answer to the question )
Upvotes: 2
Reputation: 15577
Depending on how you plan to consume the information, you can choose strings, like has already been suggested, or parse it into a date then pull out the pieces.
DateTime date = DateTime.Parse("10/15/2010");
int y = date.year;
int m = date.Month;
int d = date.Day;
Upvotes: 1
Reputation: 11495
Simple:
string[] pieces = "10/15/2010".Split ('/');
Using String.Split.
Upvotes: 0
Reputation: 8620
Assuming you wanted the "split" elements to be strings as well:
string date = "10/15/2010";
string[] split = date.Split('/');
Upvotes: 0
Reputation: 5404
Take a look at String.Split().
string date = "10/15/2010";
string[] dateParts = date.Split('/');
Upvotes: 3
Reputation: 11343
string str = "10/15/2010";
string[] parts = str.split('/');
You now have string array parts
that holds parts of that initial string.
Upvotes: 4