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.
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".
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)
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"
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!
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);
string wordsBefore = "WordOne WordTwo WordThree";
string[] wordsWorking = wordsBefore.Split();
string wordsAfter = ("{0} {1} {2}", string.Join(" ", wordsWorking.Skip(2)), wordsWorking[0], wordsWorking[1]);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With