Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing position of words in a string

Tags:

c#

.net-3.5

I have a string let say,

string temp1 = "25 10 2012"

but I want this,

"2012 10 25"

what would be the best way of doing it. format will always be like this.

like image 948
Change Avatar asked Oct 25 '12 12:10

Change


2 Answers

Looks like its a date. You can parse the string to DateTime, using DateTime.ParseExact and then use .ToString to return formatted result.

DateTime dt = DateTime.ParseExact(temp1, "dd MM yyyy", CultureInfo.InvariantCulture);
Console.Write(dt.ToString("yyyy MM dd"));

You may use that DateTime object later in your code, and also apply different formatting (if you need)

like image 79
Habib Avatar answered Sep 30 '22 02:09

Habib


try this split string and reverse array , and this will work for string of any length ...

string[] myArray = temp1.Split(' ');
 Array.Reverse( myArray );
string reverse =string.Join(" ", myArray );
like image 30
Pranay Rana Avatar answered Sep 30 '22 02:09

Pranay Rana