Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change order of words in a string?

Tags:

string

c#

I'm a real newbie and whilst I found a question and answer quite similar (Winforms C# change string text order) I still need some help with this please, especially due to point 3 below which makes it a bit more tricky.

  1. The string is made up of 3 'Words' the order of which needs to be changed. The string always has the same pattern: "WordOne WordTwo WordThree" - some examples are "Car Storage Sydney", "Boat Storage Melbourne" and "Caravan Storage Brisbane".

  2. Each 'Word' is separated always by a single space. As I understand this would be used to split the variable up and then it could be re-formatted to achieve the desired order change (see 4 below)

  3. This is where it gets a bit tricky: The 3rd 'Word' sometimes is actually two words, ie "Word Three" instead of "WordThree" perhaps better explained with another example: "Boat Storage Gold Coast"

  4. The desired order change is "WordThree WordOne WordTwo" (basically, just moving "WordThree" from the back to the front) so using the same examples as in 1 above, the finished result would be "Sydney Car Storage", "Melbourne Boat Storage" and "Brisbane Caravan Storage". And the tricky one, based on the sometimes two-worded "Word Three", as shown in 3. above, would be "Gold Coast Boat Storage"

I hope I have explained it well enough.

Except for issue as described in 3. above, I believe it should be something like this, just my rough newbie attempt at the code:

string wordsBefore = "WordOne WordTwo WordThree";
string[] wordsWorking = wordsBefore.Split(new string[]{" "});
string wordsAfter = ("{0} {1} {2}", wordsWorking[2], wordsWorking[0], wordsWorking[1]);

I think this is pretty close?

But of course, because of issue as described in 3. above, there needs to be additional code to detect when "WordThree" contains two words and to somehow handle them as one word. I think you know what I mean!

Eagerly awaiting some assistance!

like image 976
Davey Crockett Avatar asked Mar 05 '13 16:03

Davey Crockett


3 Answers

You can tell Split to only give you a certain number of results:

string wordsBefore = "WordOne WordTwo Word Three";
string[] wordsWorking = wordsBefore.Split(new [] {' '}, 3);
string wordsAfter = string.Format("{0} {1} {2}", 
                wordsWorking[2], wordsWorking[0], wordsWorking[1]);

// result: "Word Three WordOne WordTwo"

Or if you want to be clever with String.Format:

string wordsBefore = "WordOne WordTwo Word Three";
string[] wordsWorking = wordsBefore.Split(new [] {' '}, 3);
string wordsAfter = string.Format("{2} {0} {1}", wordsWorking);
like image 95
D Stanley Avatar answered Oct 17 '22 03:10

D Stanley


string wordsBefore = "WordOne WordTwo WordThree";
string[] wordsWorking = wordsBefore.Split();
string wordsAfter = ("{0} {1} {2}", string.Join(" ", wordsWorking.Skip(2)), wordsWorking[0], wordsWorking[1]);
like image 38
Austin Salonen Avatar answered Oct 17 '22 03:10

Austin Salonen


Keep it simple.

string wordsBefore = "WordOne WordTwo WordThree";
string[] wordsWorking = wordsBefore.Split(new string[]{" "});
string word1 = wordsWorking[0];
string word2 = wordsWorking[1];
string word3 = wordsWorking[2];

if (wordsWorking.length == 4)
   word3 = word3 + " " + wordsWorking[3];

string wordsAfter = ("{0} {1} {2}", word3 word1, word2);
like image 2
Henk Holterman Avatar answered Oct 17 '22 03:10

Henk Holterman