Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All,

I have a string I need to reverse. I was looking for the simplest way (as always!)
so have tried the string to see if that exists, it doesn't or I can't find it.
MSDN have lots of suggestions along the lines of
C#
Console.WriteLine("Pleae enter a string, print enter to end");
string input = Console.ReadLine();
StringBuilder sb = new StringBuilder();
int i = input.Length-1;
while (i != - 1)
{
      sb.Append(input[i--]);
}
Console.WriteLine("Reverse stirng is " + sb.ToString());
which seems to be a the correct way of doing it I just now
need to get the sb the string builder "variable" to equal a string for the next bit of editing it need as a string for the next bit of processing.
if I do sb = Next (Next is a string) I get the Error Cannot implicitly convert type 'string' to 'System.Text.StringBuilder'. So two questions is this the right way, if so how do I get a string out it, if not what is the right way?
Glenn
Posted

LINQ could be another approach you could consider.
String Reversal[^] discusses three approaches to reverse a string.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 11:45am    
Nice and kinda comprehensive, a 5.
—SA
Abhinav S 22-Jan-13 11:56am    
Thank you.
Did you try searching? This was the first hit for 'c# string reverse' and answers your question. It seems the Array.Reverse approach is the best.
 
Share this answer
 
Comments
glennPattonWork3 22-Jan-13 11:34am    
Only on MSDN really, found a method and tried sb.ToString() sorted out my problem, I will have a look at that now....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900