开发者

How to break this string?

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!!


You probably want to call

DateTime date = DateTime.Parse("10/15/2010", CultureInfo.InvariantCulture);


string str = "10/15/2010";
string[] parts = str.split('/');

You now have string array parts that holds parts of that initial string.


Take a look at String.Split().

string date = "10/15/2010";
string[] dateParts = date.Split('/');


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 )


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;


"10/15/2010".Split('/')


Assuming you wanted the "split" elements to be strings as well:

string date = "10/15/2010";

string[] split = date.Split('/');


var date = "10/15/2010";

var split = date.Split('/')


Simple:

string[] pieces = "10/15/2010".Split ('/');

Using String.Split.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜