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.
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.
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)
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 );
You could do it using the Split command and then recombining the sub strings:
String[] subStrs = temp1.Split( ' ' );
String final = subStrs[2] + " " + subStrs[1] + " " + subStrs[0];
So you want to split words and change the order, you can use LINQ:
var words = temp1.Split(' ');
String newWord = string.Join(" ", words.Reverse());
or if you don't want to swap all words but only swap the first and last word:
String first = words.Last();
String last = words.First();
String newWord = first + " "
+ string.Join(" ", words.Skip(1).Take(words.Length - 2))
+ " " + last;
You could either use a RegEx or split the string and rejoin it in reverse order.
string s = "2012 10 25";
string[] tokens = s.Split(' ');
Array.Reverse(tokens);
string final = string.Join(" ", tokens);
If Your string has always 10 character (with spaces), You can do the following:
string str = "26 10 2012"
str = str.Substring(6, 4) + " " + str.Substring(3, 2) + " " + str.Substring(0, 2)
you can use string.split(" ").reverse().join(" ").
You can use split, this function will split your string to array on white space as condition then reverse the array and re join based on white space
let string="25 10 2012";
let output=string.split(" ").reverse().join(" ");
console.log(output)
© 2022 - 2024 — McMap. All rights reserved.