how reverse string in c# code example

Example 1: c# reverse string

public static string ReverseString(string s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

Example 2: reverse string c#

string str = "Hello World!"; // the string which you want to reverse
string reverse = "";
int length = str.Length - 1; 

while(length >= 0)
{
	reverse += str[length];
   	length--;
}

Console.WriteLine(reverse);  // output: "!dlroW olleH"

Example 3: c# substring reverse

public static string ToReverseString(this string value)
{
  return string.Join("", value.Reverse());
}

public static string SubstringReverse(this string value, int indexFromEnd, int length)
{
  return value.ToReverseString().Substring(indexFromEnd, length).ToReverseString();
}

Example 4: c# reverse a string and case

//Reverse string and reverse case
using System.Linq;
public class Hello{
    public static void Main(){
        string inputstring="AbcdeFhiJKl";
        char[] arr = new char[inputstring.Length];
        int i=0;
        foreach (char v in inputstring.Select(x => x).Reverse())
        {
            if (char.IsUpper(v)) { arr[i]=char.ToLower(v); }else{ arr[i]=char.ToUpper(v);};
            i=i+1;
        }
        string str = new string(arr);
        System.Console.WriteLine(str);
        System.Console.ReadLine();
    }
}