EN
C# / .NET - reverse words in a given string
0
points
In this article, we would like to show you how to reverse words in a given string in C#.
Practical example
In the example below, we use the Array.Reverse()
method, which argument is a string converted into an array of words.
using System;
public class Program
{
static string ReverseWords(string originalString)
{
string[] arrayOfWords = originalString.Split(" ");
Array.Reverse( arrayOfWords );
return String.Join(" ", arrayOfWords);
}
public static void Main(string[] args)
{
string originalString = "This is your text...";
string reversedWordsString = ReverseWords(originalString);
Console.WriteLine(reversedWordsString); // text... your is This
}
}
Output:
text... your is This