How to move the first word to the end of a string in C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    public static string move_first_word_to_end_of_string(string s)
    {
        var parts = s.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        if (parts.Length <= 1)
            return s.Trim();

        // Join all except the first, then append the first at the end
        return string.Join(" ", parts.Skip(1)) + " " + parts[0];
    }

    static void Main()
    {
        string s = "Would you like to know more? (Explore and learn)";
        string result = move_first_word_to_end_of_string(s);

        Console.WriteLine(result);
    }
}



/*
run:

you like to know more? (Explore and learn) Would

*/

 



answered Feb 5 by avibootz
...