0

I have a StringBuilder result and I want to reverse it but in C# the StringBuilder doesn't support .Reverse() so first I cast it to a string and then I reverse it and add a "$" to the end of string.

but the answer is like this:

System.Linq.Enumerable+<ReverseIterator>d__75`1[System.Char]"   string

how can I fix this?

StringBuilder result = new StringBuilder();
List<string> matrix = new List<string>();
List<int> indexes = new List<int>();
for (int i = 0; i < bwt.Length; i++)
{
    matrix.Add("" + bwt[i]);
    indexes.Add(i);
}
indexes.Sort((o1, o2) => matrix[o1].CompareTo(matrix[o2]));
int current = indexes[0];
for (int i = 0; i < bwt.Length - 1; i++)
{
    int index = indexes.IndexOf(current);
    string next = bwt[index].ToString();
    result.Append(next);
    current = index;
}
// return result.ToString().ToString() + "$";
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • You are getting the object name and not the string. Instead of cast use the ToString() method. Since you have a list of strings what also can work is the join method with an empty join string : string.Join("", indexes); – jdweng Apr 13 '19 at 12:38
  • Can you give us value present in `result`? – Prasad Telkikar Apr 13 '19 at 12:38
  • as @jdweng suggests and this answer https://stackoverflow.com/a/228060/3225 – kenny Apr 13 '19 at 12:42
  • it will returned "System.Linq.Enumerable+d__75`1[System.Char]$" string – mobiaaaaaaaam Apr 13 '19 at 12:50
  • @jdweng indexes give me a inverseBwt and i can not join them like this,can you expalin me more?even with string.join("",indexes) reversing doesn't work – mobiaaaaaaaam Apr 13 '19 at 16:23

2 Answers2

4

StringBuilder allows you to access and manipulate the characters through their indexes.

string stringToReverse = "abcdefghijk";
var sb = new StringBuilder(stringToReverse);
for (int i = 0, j = sb.Length - 1; i < sb.Length / 2; i++, j--) {
    char temp = sb[i];
    sb[i] = sb[j];
    sb[j] = temp;
}
string reversed = sb.ToString();

But I made a Test which shows that @Mendhak's version is faster. One million repetitions with strings of length 100

StringBuilder: 974 ms
Mendhak:       209 ms

So on my PC it takes just 209 ns to reverse the string with Mendhak's solution. Just because a solution looks faster it must not be faster. Array.Reverse calls the external method private static extern bool TrySZReverse(Array array, int index, int count); which is highly optimized.

The test:

static class ReverseString
{
    const string stringToReverse = "abcdex.dfkjvhw4o5i8yd,vfjhe4iotuwyflkcjadhrlniqeuyfmln mclf8yuertoicer,tpoirsd,kfngoiw5r8t7ndlrgjhfg";

    public static string TestStringBuilder()
    {
        var sb = new StringBuilder(stringToReverse);
        for (int i = 0, j = sb.Length - 1; i < sb.Length / 2; i++, j--) {
            char temp = sb[i];
            sb[i] = sb[j];
            sb[j] = temp;
        }
        return sb.ToString();
    }

    // By Mendhak
    public static string TestArrayReverse()
    {
        char[] arr = stringToReverse.ToString().ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

    public static void Compare()
    {
        string result1 = TestStringBuilder();
        string result2 = TestArrayReverse();
        Console.WriteLine($"Are result1 and 2 equal?   {result1 == result2}");

        Measure("StringBuilder", TestStringBuilder);
        Measure("Array.Reverse", TestArrayReverse);
        Console.ReadKey();
    }

    public static void Measure(string method, Func<string> sut)
    {
        const int NTests = 1000000;

        var stopWatch = new Stopwatch();
        stopWatch.Start();
        for (int i = 0; i < NTests; i++) {
            sut();
        }
        stopWatch.Stop();
        Console.WriteLine($"{method} = {stopWatch.ElapsedMilliseconds} ms");
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
3

You will need to take a few extra steps to reverse your string. Build it as you normally would in your stringbuilder (result), then

char[] arr = result.ToString().ToCharArray();
Array.Reverse(arr);
return new string(arr) + "$";
Mendhak
  • 8,194
  • 5
  • 47
  • 64