Davey Crockett
Davey Crockett

Reputation: 79

How to change order of words in a string?

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!

Upvotes: 7

Views: 6436

Answers (5)

Henk Holterman
Henk Holterman

Reputation: 273199

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);

Upvotes: 2

Cerbi
Cerbi

Reputation: 41

Here is my example:

string text = "word one<splittag>word two and some other words<splittag>word three with   some text";
        string[] words = text.Split(new string[] { "<splittag>" }, StringSplitOptions.None);
        Label1.Text = String.Format("{2},{0},{1}", words[2], words[0], words[1]);

Hope it helps!

Upvotes: 0

D Stanley
D Stanley

Reputation: 152521

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);

Upvotes: 6

Petrutiu Mihai
Petrutiu Mihai

Reputation: 575

const string wordsBefore = "WordOne WordTwo Word Three";
string[] wordsWorking = wordsBefore.Split();
var thirdWord = wordsWorking.Skip(2).Aggregate((s, s1) => s + " " + s1);    
string wordsAfter = string.Format("{0} {1} {2}", thirdWord, wordsWorking[0], wordsWorking[1]);

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50215

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

Upvotes: 3

Related Questions