编写一个C#代码,返回一个字符串,每个字母都反转

时间:2011-09-16 14:46:00

标签: c# c#-4.0

我们在C#中有任何适当的功能吗?

 private void button1_Click(object sender, EventArgs e)
 {
     string s1 = textBox1.Text;
 }

2 个答案:

答案 0 :(得分:7)

string s1 = new string(
    textBox1.Text.Select(ch => char.IsLetter(ch) ? (char.IsUpper(ch) ?
                      char.ToLower(ch) : char.ToUpper(ch)) : ch).ToArray());

答案 1 :(得分:1)

public string RevString()
{
    var s = "TestThisString";
    StringBuilder sb = new StringBuilder();
    var arr = s.ToCharArray();

    foreach (var c in arr)
    {
        if (c.ToString() == c.ToString().ToLower())
        { sb.Append(c.ToString().ToUpper());}
        else
        { sb.Append(c.ToString().ToLower()); }
    }
    return sb.ToString();
}