如何使用C#反转字符串?

如何使用C#反转字符串?

要反转一个字符串,使用 Array.Reverse() 方法。

设置要反转的字符串 −

string str = "Amit";

登录后复制

在上述方法中,我们将字符串转换为字符数组 −

char[] ch = str.ToCharArray();

登录后复制

然后使用Reverse()方法。

Array.Reverse(ch);

登录后复制

Example

的中文翻译为:

示例

using System;
namespace Demo {
class Program {
static void Main(string[] args) {

string str = "Amit";
char[] ch = str.ToCharArray();

Array.Reverse(ch);

foreach(var items in ch) {
Console.WriteLine(items);
}

Console.ReadLine();
}
}
}

登录后复制

以上就是如何使用C#反转字符串?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!