Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to reverse stringbuilder result [duplicate]

Tags:

c#

I'm trying to reverse and return a StringBuilder result but I get the following errors for trying all variations in order of return sb.Reverse().ToString():

  1. Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string'.

  2. 'StringBuilder' does not contain a definition for 'Reverse' and no extension method 'Reverse' accepting a first argument of type 'StringBuilder' could be found

What is the proper format for returning the stringbuilder result in reverse order?

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

This bit of code unfortunately doesn't do it one line

Please vote to delete this post so I can get rep back XD

like image 243
springathing Avatar asked May 29 '26 11:05

springathing


1 Answers

You have to turn it into a string, then reverse it, turn it into an array, then create a new string from that:

return new string(sb.ToString().Reverse().ToArray());
like image 180
itsme86 Avatar answered Jun 01 '26 02:06

itsme86



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!