我刚刚在c# 2.0中写了一个字符串反向函数(即LINQ不可用),然后想到了这个:

public string Reverse(string text)
{
    char[] cArray = text.ToCharArray();
    string reverse = String.Empty;
    for (int i = cArray.Length - 1; i > -1; i--)
    {
        reverse += cArray[i];
    }
    return reverse;
}

就我个人而言,我并不喜欢这个功能,我相信有更好的方法来实现它。是吗?


当前回答

我们可以使用两个指针,一个指向字符串的开始,另一个指向字符串的结束。然后每次交换第i个和第j个指针的值,第i个指针的值递增+1,第j个指针的值递减-1。

string s = Console.ReadLine();
Console.WriteLine(s + "\n");
char[] charArray = s.ToCharArray();
int i = 0, j = s.Length - 1;
while (i < j) {
    char temp = charArray[i];
    charArray[i] = charArray[j];
    charArray[j] = temp;
    i++; j--;
}
string ans = new string(charArray);
Console.WriteLine(ans + "\n");
// Input: hello
// Output: olleh

其他回答

反转字符串,甚至不使用新字符串。让说

String input  = "Mark Henry";
//Just to convert into char array. One can simply take input in char array.
Char[] array = input.toCharArray(input);
int a = input.length;

for(int i=0; i<(array.length/2 -1) ; i++)
{
    array[i] = array[i] + array[a];
    array[a] = array[i] - array[a];
    array[i] = array[i] - array[a--];
}
string A = null;
//a now is reversed and you can use it
A = SimulateStrReverse.StrReverse("your string");

public static class SimulateStrReverse
{
    public static string StrReverse(string expression)
    {
        if (string.IsNullOrEmpty(expression))
            return string.Empty;

        string reversedString = string.Empty;
        for (int charIndex = expression.Length - 1; charIndex >= 0; charIndex--)
        {
            reversedString += expression[charIndex];
        }
        return reversedString;
    }
}

这里有一个解决方案,正确地将字符串“Les Mise\u0301rables”反转为“selbare\u0301siM seL”。这应该像selbarésiM seL一样呈现,而不是selbaŕesiM seL(注意重音的位置),就像大多数基于代码单元(Array。反向,等等),甚至代码点(反向时要特别注意代理项对)。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

public static class Test
{
    private static IEnumerable<string> GraphemeClusters(this string s) {
        var enumerator = StringInfo.GetTextElementEnumerator(s);
        while(enumerator.MoveNext()) {
            yield return (string)enumerator.Current;
        }
    }
    private static string ReverseGraphemeClusters(this string s) {
        return string.Join("", s.GraphemeClusters().Reverse().ToArray());
    }

    public static void Main()
    {
        var s = "Les Mise\u0301rables";
        var r = s.ReverseGraphemeClusters();
        Console.WriteLine(r);
    }
}

(和现场运行的例子在这里:https://ideone.com/DqAeMJ)

它只是简单地使用。net API进行字素簇迭代,它一直存在,但看起来有点“隐藏”。

有各种方法来反转字符串,我已经在下面展示了其中的3种。

——使用数组。反向作用。

 private static string ReverseString1(string text)
    {
        char[] rtext = text.ToCharArray();
        Array.Reverse(rtext);
        return new string(rtext);
    }

——只使用字符串

  private static string ReverseString2(string text)
    {
        String rtext = "";
        for (int i = text.Length - 1; i >= 0; i--)
        {
            rtext = rtext + text[i];
        }
        return rtext;
    }

——只使用char数组

 public static string ReverseString3(string str)
    {
        char[] chars = str.ToCharArray();
        char[] rchars = new char[chars.Length];
        for (int i = 0, j = str.Length - 1; i < chars.Length; i++, j--)
        {
            rchars[j] = chars[i];
        }
        return new string(rchars);
    }

我们可以使用两个指针,一个指向字符串的开始,另一个指向字符串的结束。然后每次交换第i个和第j个指针的值,第i个指针的值递增+1,第j个指针的值递减-1。

string s = Console.ReadLine();
Console.WriteLine(s + "\n");
char[] charArray = s.ToCharArray();
int i = 0, j = s.Length - 1;
while (i < j) {
    char temp = charArray[i];
    charArray[i] = charArray[j];
    charArray[j] = temp;
    i++; j--;
}
string ans = new string(charArray);
Console.WriteLine(ans + "\n");
// Input: hello
// Output: olleh