以下代码实现了此功能:
string word = "today is beautiful very much";
string[] wordArr = word.Split(' '); //按空格分割字符串,取出各单词放数组中
List<string> list = new List<string>(); //list用来保存单词
for (int i = wordArr.Length - 1; i >= 0; i--) //从后往前遍历数组,将单词放list中,相当于逆序了
{
list.Add(wordArr[i]);
}
word = string.Join(" ", list); //将list中的元素用空格连接起来
Console.WriteLine(word);
Console.ReadKey(); |