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() + "$"; 
5
  • 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); Commented Apr 13, 2019 at 12:38
  • Can you give us value present in result? Commented Apr 13, 2019 at 12:38
  • as @jdweng suggests and this answer stackoverflow.com/a/228060/3225 Commented Apr 13, 2019 at 12:42
  • it will returned "System.Linq.Enumerable+<ReverseIterator>d__75`1[System.Char]$" string Commented Apr 13, 2019 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 Commented Apr 13, 2019 at 16:23

2 Answers 2

5

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; // Since C# 7.0 you can use tuples to swap two values: // (sb[i], sb[j]) = (sb[j], sb[i]); } 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"); } } 
Sign up to request clarification or add additional context in comments.

Comments

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) + "$"; 

1 Comment

it takes a lot of time,what's another fast way?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.