Changing position of words in a string
Asked Answered
I

7

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.

Implantation answered 25/10, 2012 at 12:19 Comment(7)
possible duplicate of Best way to reverse a stringJuneberry
Do you always want to reverse a strngs or reverse word order??Barcus
@PLB yup format is always fixedImplantation
Are you sure you are no looking for DateTime parsing and/or formatting?Intolerance
Nope, as I was getting string not date from a method I can't make changes to.Implantation
@Ignacio: Kris asked if you want to change a date format that comes as a string.Exponible
@TimSchmelter if thats the best option then why not :)Implantation
E
11

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)

Esdras answered 25/10, 2012 at 12:21 Comment(0)
R
2

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 );
Ratcliffe answered 25/10, 2012 at 12:22 Comment(0)
E
1

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];
Evanesce answered 25/10, 2012 at 12:21 Comment(1)
@PranayRana: Well he states in his comments that the format is always the same ...Evanesce
E
1

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;
Exponible answered 25/10, 2012 at 12:22 Comment(0)
P
0

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);
Pernod answered 25/10, 2012 at 12:21 Comment(0)
P
0

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)
Pigment answered 25/10, 2012 at 12:22 Comment(0)
M
0

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)
Mawkish answered 8/12, 2021 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.